9

I have a template which allows the user to edit their user information.

<form method="post">
    <table>
        <tr>
            <td>Username:</td>
            <td>{{user['username']}}</td>
        </tr>
        <tr>
            <td>New Password:</td>
            <td> <input type="password" name="password"></td>
            <td>{% if form.password.errors %} {{form.password.errors}} {% endif %}<td>
        </tr>
        <tr>
            <td>Re-enter Password:</td>
            <td> <input type="password" name="confirm_password">
            </td>
        </tr>
        <input type='hidden' name='username' value="{{user['username']}}">
        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
</form>

I also have a view function for handling such edits by the user. The database I am currently using is MongoDB with the MongoKit module. I have only been able to do up to this so far in the view function, yet with no luck.

def edit():
    username = request.args.get('user')
    user = User.find_one({'username':username}) # Is this a correct way of doing it?
    form = UserForm(**what should be placed here?**, obj=user)

    if request.method == 'POST' and form.validate():
        form.populate_obj(user)
        user.save()
        return 'updated'
    return render_template('edituser.html', form=form, user=user)

I am going through populate_obj(obj) for this purpose. I couldn't find much help in this matter. What should I do in order to get populate_obj() working?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
consumer
  • 709
  • 3
  • 7
  • 19
  • You need to describe what error you are getting, or how the results you are getting differ from your expectations. – Vinay Sajip Jun 01 '11 at 06:58
  • I was hoping if anyone would point me out on what should i be keeping where i have written **what should be placed here?**. Also about the user object obtained from that way is allowed to pass or not. – consumer Jun 01 '11 at 09:35

3 Answers3

17

UserForm should have request.form passed into it to populate it with the values available in the POST request (if any).

form = UserForm(request.form, obj=user)
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • 9
    Only with native `WTForms`. The extra `request.form` is not needed with `Flask-WTF`. – Sean Jun 03 '11 at 22:38
  • 6
    @Sean - True ... which is part of why `Flask-WTF` is a good extension; it helps cut out boilerplate. But the OP isn't using `Flask-WTF`, so simply recommending that he use it is somewhat akin to suggesting using jQuery to solve a JavaScript issue. It will mean that your issue is solved for you -- but the issue is solved **for** you ... **you** still don't know how to solve it. That's why I only gave the OP the way to do what he needs using Flask and WTForms in a bare-bones manner (you have already very nicely pointed out the advantages of `Flask-WTF` and I gave you a +1 for that ;-) ). – Sean Vieira Jun 03 '11 at 23:38
  • *nods* The correction was more for the google cookies than anything else. That said, if a user has a problem during development (vs maintenance), it's easier to suggest things that make life easier now vs later. :~] – Sean Jun 03 '11 at 23:41
6

Are you using Flask-WTF? If so, check out the following sample code:

https://github.com/sean-/flask-skeleton/blob/master/skeleton/modules/aaa/views.py#L13

Specifically, you would:

def edit():
    form = UserForm()
    if form.validate_on_submit():
        # Commit your form data

Bottom line, if you're using Flask-WTF, I'm not sure what your question is. If you aren't using Flask-WTF, use Flask-WTF.

Sean
  • 9,888
  • 4
  • 40
  • 43
  • "Bottom line, if you're using Flask-WTF, I'm not sure what your question is. If you aren't using Flask-WTF, use Flask-WTF." I can't agree more on that last sentence. If you don't use Flask-WTF, use Flask-WTF. – Depado Feb 18 '14 at 11:29
  • 1
    It's quite hard to see what's going on in that example, as you're doing custom SQLA stuff rather than just a normal `db.session.commit()` :) – Rob Grant Sep 09 '15 at 13:07
2

In case of Flask-WTF, you can write like

form = UserForm(obj=user)

Thant will work!

Anil Konsal
  • 151
  • 2
  • 11