Populate a PasswordField in wtforms
flask, wtforms
Solution
Something I've found with Flask, and Flask apps in general, is that the source is the documentation. Indeed, it looks like by default you cannot populate the field. You can pass an argument `hide_value` to prevent this behavior.
This is a good call, since if you can populate the field, you have access to the raw password... which could be dangerous.
class PasswordInput(Input):
"""
Render a password input.
For security purposes, this field will not reproduce the value on a form
submit by default. To have the value filled in, set `hide_value` to
`False`.
"""
input_type = 'password'
def __init__(self, hide_value=True):
self.hide_value = hide_value
def __call__(self, field, **kwargs):
if self.hide_value:
kwargs['value'] = ''
return super(
Problem
Is it possible to populate a password field in wtforms in flask? I've tried this: ``` capform = RECAPTCHA_Form() capform.username.data = username capform.password.data = password ``` The form is defined like: ``` class RECAPTCHA_Form(Form): username = TextField('username', validators=[DataRequired()]) password = PasswordField('password', validators=[DataRequired()]) remember_me = BooleanField('Remember me.') recaptcha = RecaptchaField() ``` The template looks like this: ``` <form method="POST" action=""> {{ form.hidden_tag() }} {{ form.username(size=20) }} {{ form.password(size=20) }} {% for error in form.recaptcha.errors %} <p>{{ error }}</p> {% endfor %} {{ form.recaptcha }} <input type="submit" value="Go"> </form> ``` I have tried to change the `PasswordField` to a `TextField`, and then it works. Is there some special limitation to populating PasswordFields in wtforms?