def do_form_field(parser, token):
"""
Render a WTForms form field allowing optional HTML attributes.
Invocation looks like this:
{% form_field form.username class="big_text" onclick="alert('hello')" %}
where form.username is the path to the field value we want. Any number
of key="value" arguments are supported. Unquoted values are resolved as
template variables.
"""
parts = token.contents.split(' ', 2)
if len(parts) < 2:
error_text = '%r tag must have the form field name as the first value, followed by optional key="value" attributes.'
raise template.TemplateSyntaxError(error_text % parts[0])
html_attrs = {}
if len(parts) == 3:
raw_args = list(args_split(parts[2]))
if (len(raw_args) % 2) != 0:
raise template.TemplateSyntaxError('%r tag received the incorrect number of key=value arguments.' % parts[0])
for x in range(0, len(raw_args), 2):
html_attrs[str(raw_args[x])] = Variable(raw_args[x + 1])
return FormFieldNode(parts[1], html_attrs)
评论列表
文章目录