Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Guled Ahmed
Guled Ahmed
12,806 Points

My "edit post" feature doesn't seem to be working properly....

I recently had implemented a few more properties to my Post model, and decided to make an option where the user can edit their posts. My current method works to an extent. When you go to the edit page you can see the name repopulate, the url repopulate, but the category (SelectField) and content (TextField) seem to be None/Blank. Is my method for repopulating a form with a posts properties incorrect? It seems to work at a certain extend, but doesn't repopulate everything:

@app.route('/edit_post/<int:post_id>', methods=("GET","POST"))
@login_required
def edit_my_post(post_id):
    posts = models.Post.select().where(models.Post.id == post_id)
    if posts.count() == 0:
        abort(404)



    post = models.Post.select().where(models.Post.id == post_id).get()
    post_user = models.Post.select().where(models.Post.user == post.user).get()

    if post_user.id == current_user.id :
        post = models.Post.select().where(models.Post.id == post_id).get()
        form = forms.ProductForm(obj=post)

        import pdb; pdb.set_trace()

        if form.validate_on_submit():
            form.populate_obj(post)
            query = models.Post.update(user = post.user.id,
                               name = form.name.data.strip(),
                               content = form.details.data.strip(),
                               url = form.url.data,
                               category = dict(forms.CATEGORIES).get(form.category.data)
                              ).where(models.Post.id == post_id)
            query.execute()
            flash("Your post has been edited.", "success")
            return redirect(url_for('index'))
    else:
        flash("Ay! Stay away from that post!","warning")
        return redirect(url_for('index'))

    return render_template("edit.html",form=form)