Вопрос

I defined a form in the following way:

class LoginForm(Form):
    login = EmailField(u'Email address', [required(), length(min=5, max=2048), validators.Email()])
    password = PasswordField(u'Password', [required(), length(min=6, max=50)])
    next = HiddenField()
    remember = BooleanField('Remember me')
    submit = SubmitField('Login')

Then I'm writing a generic macro in Jinja2 to render the form fields and I would like to do something like:

{% if field.is_required() %}
  {{ field.label(class_='required') }}
{% else %}
  {{ field.label() }}
{% endif %}

So... is there a way to see if a field is required?

Это было полезно?

Решение

Validators can set flags which you can check for:

{% if field.flags.required %}field.label(class_='required'){% endif %}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top