Python Forum
Pre-populating WTForms form values for edit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pre-populating WTForms form values for edit
#1
I'm a newbie, trying to use Flask with WTForms. My objective is to create a form to edit a user record returned from a database. The following code, mocking the db with an instance of the User class, executes without error for the /form route. No problem. But the form values do not contain pre-populated data for the user to edit!

The /test route returns: TypeError: 'str' object is not callable.

I just want the form to display user data for editing.
Any help would be greatly appreciated.

Here's code from my .py file:

class LogInForm(FlaskForm):
    username = StringField('username', [validators.DataRequired()])
    password = PasswordField('password', [validators.DataRequired()])

class User():
    def __init__(self, name, pwd):
        self.name = name
        self.pwd = pwd

@app.route('/test', methods=['GET', 'POST'])
def test():
    found_user = User("Moe", "2222")
    form = LogInForm(obj=found_user)
    return render_template('test.html',user=found_user, form=form)


@app.route('/form', methods=['GET', 'POST'])
def form():
    user = User("Moe", "2222")

    form = LogInForm(obj=user)

    if form.validate_on_submit():
        return f'The form has been submitted. Username {form.username.data}. Password{form.password.data}'
    return render_template('forms.html', form=form)
Here's the template for the /form route:

form  method = "POST" action="{{ url_for('form') }}">
    {{ form.csrf_token }}
    {% for field in form %}
    <p>
        {{field.label}}
        {{field}}
    </p>
    {% endfor %}
    <input type="submit" value="Submit">
</form>
</body>
Here's the template for the /test route:

<body>
    {% from "_formhelpers.html" import render_field %}
    <form method="post">
        <dl>
            {{ render_field(user.name) }}
            {{ render_field(user.pwd) }}
        </dl>
        <p><input type="submit" value="Submit">
    </form>
<a href="/">Go to home page.</a>
</body>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Web scraper not populating .txt with scraped data BlackHeart 5 1,510 Apr-03-2023, 05:12 PM
Last Post: snippsat
  Jinja sort values from request.form SpongeB0B 2 2,221 Jul-26-2020, 07:41 AM
Last Post: SpongeB0B
  Edit Form using Selenium Prince_Bhatia 2 3,930 Feb-14-2018, 10:31 PM
Last Post: snippsat
  Passing Web Form Values VSPythonD3v 8 5,726 Mar-26-2017, 07:01 PM
Last Post: VSPythonD3v

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020