微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据库没有保存在我部署在 Heroku 上的 Django 应用程序中

如何解决数据库没有保存在我部署在 Heroku 上的 Django 应用程序中

我最近在 heroku 上使用 django 部署了我的投资组合。该应用程序正在从 github 设置自动部署。它包含一个看起来像这样的评论框。您可以在 --> https://anujdhillon.herokuapp.com/ 亲眼看到

Sample picture

GET 和 POST 请求都运行良好,即当我发布新评论时,它确实显示评论列表中。但是,几个小时后,所有发布的新评论都会自动删除,我无法弄清楚为什么会发生这种情况。 django 源代码 --> https://github.com/anujdhillon/my-portfolio

组件是用ReactJS编写的,下面是它的源码 -->

import React from 'react';

class CommentSection extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            commentList:[],activeItem: {
                id:null,content: '',author: '',},editing: false,}
        this.fetchComments = this.fetchComments.bind(this);
        this.handleContentChange = this.handleContentChange.bind(this);
        this.handleAuthorChange = this.handleAuthorChange.bind(this);
        this.postComment = this.postComment.bind(this);
        this.getCookie = this.getCookie.bind(this)

    };

    getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i].trim();
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0,name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    componentDidMount(){
        this.fetchComments();
    }

    fetchComments(){
        fetch('https://anujdhillon.herokuapp.com/api/comment-list/')
        .then(response => response.json())
        .then(data =>
            this.setState({
                commentList: data
            }
            )
        )
    }

    handleContentChange(e){
        var name = e.target.name;
        var value = e.target.value;
        this.setState({
            activeItem:{
                ...this.state.activeItem,content:value
            }
        })
    }
    handleAuthorChange(e){
        var name = e.target.name;
        var value = e.target.value;
        this.setState({
            activeItem:{
                ...this.state.activeItem,author:value
            }
        })
    }

    postComment(){
        var csrftoken = this.getCookie('csrftoken')
        var url = 'https://anujdhillon.herokuapp.com/api/comment-create/'
        fetch(url,{
            method: 'POST',headers:{
                'Content-type': 'application/json','X-CSrftoken':csrftoken,body:JSON.stringify(this.state.activeItem)
        }).then((response) => {
            console.log(this.state.activeItem)
            this.fetchComments()
            this.setState({
                activeItem: {
                    id:null,}
            })
        }).catch(function(error){
            console.log("Error: ",error)
        })
    }



    render() { 
        var comments = this.state.commentList;
        return (
        <div className="comment-Box">
                <div className="write-area">
                    <div className="inputtext">
                        <textarea type="text"  maxlength="200" id="content" name="content" placeholder="Write comment..." value = {this.state.activeItem.content} onChange={this.handleContentChange}/>
                        <textarea type="text"  maxlength="200" id="author" name="author" placeholder="Write your name..." value = {this.state.activeItem.author} onChange={this.handleAuthorChange}/>
                    </div>
                    <div className="submit-button">
                        <button onClick={ () => this.postComment() } className="btn">Send</button>
                    </div>
                </div>
                <div className="display-area">
                {
                    comments.map((item) =>{
                        return <div className="comment">
                                <div className="comment-info">
                                    <span className="comment-author">{ item.author }</span>
                                    <p>
                                        {item.date_created}
                                    </p>
                                </div>
                                <div className="comment-body">
                                    <p>
                                    { item.content}
                                    </p>
                                </div>            
                   </div>
                    })
                }
                </div>
            </div>
        )
    }
}
export default CommentSection;

提前致谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?