H
H
hardwellZero2015-03-08 12:42:44
Flask
hardwellZero, 2015-03-08 12:42:44

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)

models.py
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

1 answer(s)
P
pcdesign, 2015-03-08
@hardwellZero

Ну как-то так:

def __init__(self, text=None, writer=None,  key=None):
        self.text = text
        self.writer = current_user.id
        self.key = key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question