flask - Rendering a WTForms CheckboxInput in Jinja Template -
i can't seem figure out how render wtforms checkboxinput
in template. when try render field using flask in jinja template error:
typeerror: call() takes 2 arguments (1 given)
the error has how {{ form.prefs(value='n') }}
used in template. wtforms documentation checkboxinput
says "the value= html attribute default ‘y’ unless otherwise specified value= @ rendering." error whether specify value or not.
i can't seem find example of how render simple checkboxinput. appreciated.
here's form:
class preferencesform(form): prefs = widgets.checkboxinput()
here's template:
{% extends "base.html" %} {% block content %} <form method="post" action="/user/prefs/"> <div>{{ form.prefs(value='n') }}</div> <button type="submit" class="btn">submit</button> </form> {% endblock %}
you supposed use booleanfield
instead of directly using widget:
class preferencesform(form): prefs = booleanfield()
and in template:
{{ form.prefs(value='n') }}
the general idea use 1 of fields in form class, automatically assign proper widget display. , widgets are:
... classes purpose render field usable representation, xhtml. when field called, default behaviour delegate rendering widget. abstraction provided widgets can created customize rendering of existing fields.
emphasis mine
further, widget needs field instance render itself:
def __call__(self, field, **kwargs): if getattr(field, 'checked', field.data): kwargs['checked'] = true return super(checkboxinput, self).__call__(field, **kwargs)
Comments
Post a Comment