Answer the question
In order to leave comments, you need to log in
How to get post id?
Hello.
I am adding comments to the posts of a small blog, but I cannot get and enter the post id into the table with comments.
What do you advise?
views.py
@users_blueprint.route('/post/<int:post_id>', methods=['GET', 'POST'])
def post(post_id):
form = CommentaryForm()
if form.validate_on_submit():
commentary = Commentary(
text = form.text.data,
key = post_id
)
db.session.add(commentary)
db.session.commit()
return render_template('post.html')
post_page = BlogPost.query.filter_by(id=post_id).first()
return render_template('post.html', post_id=post_id, post_page=post_page, form=form)
class Commentary(db.Model):
__tablename__ = "commentary"
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String, nullable=False)
writer = db.Column(db.Integer, ForeignKey('users.id'))
key = db.Column(db.Integer, ForeignKey('posts.id'))
def __init__(self, text):
self.text = text
self.writer = current_user.id
def __repr__(self):
return '<text {}'.format(self.text)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question