flask-wtforms必填字段 [英] flask-wtforms field required
问题描述
如何在此烧瓶代码上添加所需的标签:
how i can add tag required on this flask code :
{{ form.youtube_href(type='url', class='form-control') }}
实际输出为:
<input class="form-control" id="youtube_href" name="youtube_href" value="" type="url">
需要此输出bat给出错误:
need this output bat give error :
<input class="form-control" id="youtube_href" name="youtube_href" value="" type="url" required>
我尝试过这个蝙蝠给错误:
im tried this bat give error :
{{ form.youtube_href(type='url', class='form-control', 'required') }}
推荐答案
从WTForms 2.2(2018年6月2日)开始,如果字段具有设置了required
标志的验证器,则字段现在呈现required
属性.作为DataRequired
和InputRequired
.如果由于某种原因您不想渲染该属性,则可以传递required=False
.或者,如果要禁用所有浏览器验证,则可以在form
标记中设置novalidate
属性.通常,您应该宁愿使浏览器验证保持启用状态,因为这会阻止对简单验证的请求/响应,这是合乎需要的.
As of WTForms 2.2 (June 2nd, 2018), fields now render the required
attribute if they have a validator that sets the required
flag, such as DataRequired
and InputRequired
. If for some reason you don't want to render the attribute, you can pass required=False
. Or if you want to disable all browser validation, you can set the novalidate
attribute in the form
tag. In general you should prefer to leave browser validation enabled, because it prevents a request/response for simple validation, which is desirable.
您在关键字参数之后传递了位置参数,这是一种语法错误.而是传递required=True
,这将在标记上设置裸属性.检查字段上的标志,以查看是否设置了Required
验证器:field.flags.required
是布尔值.创建一个URLField
而不是手动传递类型.
You are passing a positional argument after keyword arguments, which is a syntax error. Instead, pass required=True
, which will set a bare attribute on the tag. Check the flags on a field to see if a Required
validator was set: field.flags.required
is a boolean. Create a URLField
rather than passing the type manually.
from flask_wtf import Form
from wtforms.fields.html5 import URLField
from wtforms.validators import InputRequired
class MyForm(Form):
youtube_href = URLField(validators=[InputRequired()])
form = MyForm()
print(form.youtube_href(required=form.youtube_href.flags.required))
# <input id="youtube_href" name="youtube_href" required type="url" value="">
这篇关于flask-wtforms必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!