V
V
Vlad2019-09-06 23:15:23
Flask
Vlad, 2019-09-06 23:15:23

Why doesn't post editing work?

Good afternoon! I use the form.populate_obj(post) method. but after editing the post, I will be redirected again to the form for editing this post, but which remained unchanged.
Here is the code of the function that should edit the post.

@posts.route('/<slug>/edit/', methods = ['POST', 'GET'])
def edit_post(slug):
    post = Post.query.filter(Post.slug ==slug).first()

    if request.method ==' POST' :
        form = PostForm(formdata=request.form, obj =post)
        form.populate_obj(post)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('posts.post_detail', slug = post.slug, ))

    form = PostForm(obj=post)
    return render_template('posts/edit.html', post=post, form = form)

Please tell me why my post editing is not working.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-09-15
@skipirich

In my opinion, the form is rendered by a get request and not a post. Try moving this part of
form = PostForm(formdata=request.form, obj =post)
before if request.method ==' POST' :

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question