我尝试输入发布内容的用户的帖子ID,并在代码行中:comment = Comments(comment = msg,author = current_user._get_current_object(),post_id),coment和author成功输入数据库,但对于post_id值isnull,如何根据用户帖子id从用户帖子输入id
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
@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)
<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>
@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)