Password filling in wtforms

Is it possible to fill in the password field in wtforms in the bulb?

I tried this:

capform = RECAPTCHA_Form() 
capform.username.data = username
capform.password.data = password

The shape is defined as follows:

class RECAPTCHA_Form(Form):
    username = TextField('username', validators=[DataRequired()])
    password = PasswordField('password', validators=[DataRequired()])
    remember_me = BooleanField('Remember me.')
    recaptcha = RecaptchaField()        

The template is as follows:

<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 tried changing PasswordFieldto TextField, and then it works.

Is there any special restriction to populate PasswordFields in wtforms?

+3
source share
3 answers

Something I found with Flask and Flask applications in general is that the source is documentation. Indeed, it seems that by default you cannot fill in the field. You can pass an argument hide_valueto prevent this behavior.

, , ... .

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(
+3

, , hide_value. , :

from flask import request    

capform = RECAPTCHA_Form(request.form) 
capform.username.data = username
capform.password.data = password

.

+2

: WTForms . arg.

from wtforms import StringField
from wtforms.widgets import PasswordInput

class MyForm(Form):
     # ...

     password = StringField('Password', widget=PasswordInput(hide_value=False))

yuji-tomita-tomita, PasswordInput () hide_value, PasswordField () PasswordInput. PasswordField, PasswordInput hide_value=False:

from wtforms import widgets
from wtforms.fields.core import StringField


class PasswordField(StringField):
    """
    Original source: https://github.com/wtforms/wtforms/blob/2.0.2/wtforms/fields/simple.py#L35-L42

    A StringField, except renders an ``<input type="password">``.
    Also, whatever value is accepted by this field is not rendered back
    to the browser like normal fields.
    """
    widget = widgets.PasswordInput(hide_value=False)
0

All Articles