0

I tried to enter the post id of the user who posted something, and in the line of code: comment = Comments (comment = msg, author = current_user._get_current_object (), post_id), coment and author successfully entered into the database, but for post_id the value isnull, how how to enter id from user post according to user post id

DATABASE

class User(db.Model, UserMixin):
    __searchable__ = ["username"]
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True,unique=True)
    password = db.Column(db.String(64), index=True,unique=True)
    posts = db.relationship('Post',backref='author', lazy='dynamic')
    comments = db.relationship('Comments',backref='author', lazy='dynamic')

    def __repr__(self):
        return self.username

class Post(db.Model):
    __searchable__ = ["body"]
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime)
    user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    comments = db.relationship("Comments",backref='post', lazy='dynamic')

    def __repr__(self):
        return self.body

class Comments(db.Model):
    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True)
    user_send_comment = db.Column(db.Integer,db.ForeignKey('user.id'))
    comment = db.Column(db.String(500))
    post_id = db.Column(db.Integer,db.ForeignKey('post.id'))

    def __repr__(self):
        return self.comment

ROUTE

@app.route("/comment/<int:id>", methods=['GET','POST'])
@login_required
def comment(id):
    post = Post.query.get(id)
    form = CommentForm()
    if form.validate_on_submit():
        return redirect(url_for('comment',id=post.id, page=1))
    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = (post.comments.count() - 1) / \
            COMMENTS_PER_PAGE + 1
    pagination = post.comments.paginate(page,per_page=COMMENTS_PER_PAGE, error_out=False)
    comments = pagination.items
    return render_template("comment.html",id=post.id, post=post, comments=comments,pagination=pagination, page=1, form=form)

CLIENT

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script type="text/javascript"src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>

<script type="text/javascript" charset="utf-8">

    $(document).ready(function() {

        namespace = "/comment";

        var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);

        socket.on('connect', function() {

            socket.emit('my event', {

                Data: 'User Has Connected!',

                Username: '{{ current_user.username }}',

                User_ID: '{{ current_user.id }}',

                Post_ID: '{{ post.id }}'

            });

        });

        socket.on('message', function(msg) {

            var postid = '{{ post.id }}'

            $('#log').append('<li>' + '{{ current_user.username }}' + ': ' + msg + '</li>');

        });

        $('#sendbutton').click(function(msg) {

            socket.send($('#myMessages').val());

            return false;

        });

    });

</script>

 

 

SERVER

@socketio.on('connect', namespace='/comment')  
def connect():
    emit ("Data", "Username") 

@socketio.on('message', namespace='/comment')
def on_comment(msg):
    print ('Sending Comment: ' + str(msg))
    comment = Comments(comment=msg, author=current_user._get_current_object(), post_id=post.author.username)
    db.session.add(comment)
    db.session.commit()
    send (msg, broadcast=True)  

@socketio.on('myevent', namespace='/comment') 
def on_broadcast(msg):
    print ("Sending Broadcast: " + str(msg))
    emit ('my response', msg, broadcast=True)
  • Wow, this is just terrible. Please reformat this entire question – kstullich Nov 02 '17 at 17:47
  • meaning? i dont know – user8489260 Nov 02 '17 at 18:31
  • In the code that you posted, `post` is an undefined symbol. This is clearly not the complete code that you are running, so it's really hard to understand the problem. Also as indicated above, please use code blocks and not quote styling to format code properly in your question. – Miguel Grinberg Nov 03 '17 at 01:15
  • yes i have edited the block of code. and is there any way to solve this problem? I have tried to fix the error in more than 1 week but still can not work, please help me solve it. – user8489260 Nov 04 '17 at 21:32

0 Answers0