Skip to main content

Jinja-WTForms

Extract WTForms classes from jinja templates

Installation

pip install jinja-wtforms

Setup

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('templates'))
env.add_extension('jinja_wtforms.WtformExtension')

Defining forms in templates

Defining forms is almost like using a pre-defined form but with added information on the type of the field.

To do so, you'll need to call a method named after the type of the field on each field. So if you want to define a "firstname" field as a text field, you can do form.firstname.text().

Let's define a signup form:

<form action="" method="post">
    {{ form.hidden_tags() }}
    <p><label>First name</label> {{ form.firstname.text() }}</p>
    <p><label>Last name</label> {{ form.lastname.text() }}</p>
    <p><label>Email</label> {{ form.email.email() }}</p>
    <p><label>Password</label> {{ form.password.password() }}</p>
</form>

The optional parameters of the field definition functions are:

  • label: the field's label (can also be define as the first argument)
  • description: the field's description
  • placeholder: the field's placeholder
  • required: boolean, default false
  • optional: boolean, default false
  • range: a tuple of (min, max), value should be a number in the range
  • length: a tuple of (min, max), value should be of string of length in the range
  • validators: a list of validator names from wtforms.validators

Available field types and their actual class:

  • checkbox: wtforms.fields.BooleanField
  • decimal: wtforms.fields.DecimalField
  • date: wtforms.fields.DateField
  • datetime: wtforms.fields.DateTimeField
  • float: wtforms.fields.FloatField
  • int: wtforms.fields.IntegerField
  • radio: wtforms.fields.RadioField
  • select: wtforms.fields.SelectField
  • selectmulti: wtforms.fields.SelectMultipleField
  • text: wtforms.fields.StringField
  • textarea: wtforms.fields.TextAreaField
  • password: wtforms.fields.PasswordField
  • upload: wtforms.fields.FileField
  • hidden: wtforms.fields.HiddenField
  • datetimelocal: wtforms.fields.DateTimeLocalField
  • decimalrange: wtforms.fields.DecimalRangeField
  • email: wtforms.fields.EmailField
  • intrange: wtforms.fields.IntegerRangeField
  • search: wtforms.fields.SearchField
  • tel: wtforms.fields.TelField
  • url: wtforms.fields.URLField

Extracting forms

Use the form registry available through the forms property of the environment:

form_class = env.forms["form.html"].form # where form.html is the template filename containing the form definition
form = form_class()

# can also be directly instantiated
form = env.forms["form.html"]()

Form classes are extracted on first call to a env.forms. You can pre-register all forms using env.forms.register_all()

The form directive

The form directive can be used before any usage of the form object. It will ensure that a form object exists. It can also be used to define the class name.

As a way to ensure a form object is always available:

{% form %}
<form action="" method="post">
    <p><label>First name</label> {{ form.firstname.text() }}</p>
</form>

To customize the class name:

{% form MyForm %}
<form action="" method="post">
    <p><label>First name</label> {{ form.firstname.text() }}</p>
</form>

Class name with optional Meta property:

{% form MyForm(csrf=True) %}
<form action="" method="post">
    {{form.hidden_tags()}}
    <p><label>First name</label> {{ form.firstname.text() }}</p>
</form>

To customize the form object variable name:

{% form = f %}
<form action="" method="post">
    <p><label>First name</label> {{ f.firstname.text() }}</p>
</form>

With custom class name:

{% form MyForm = f %}
<form action="" method="post">
    <p><label>First name</label> {{ f.firstname.text() }}</p>
</form>

You can disable auto instantiation of a form object when non is present by using auto_init=False as a Meta param.

Multiple forms in a single template

Use multiple form directives to declare mutliple forms with different form object names.

{% form %}
{% form = form2 %}
<form action="" method="post">
    <p><label>First name</label> {{ form.firstname.text() }}</p>
</form>
<form action="" method="post">
    <p><label>First name</label> {{ form2.email.email() }}</p>
</form>

Then access them from the registry:

form_class = env.forms["form.html"].form
form2_class = env.forms["form.html"].form2

Metadata

Release files for jinja-wtforms 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja-wtforms 0.2.1
File Size Uploaded
jinja_wtforms-0.2.1.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja-wtforms 0.2.1
File Interpreter ABI Platform
jinja_wtforms-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / jinja_wtforms-0.2.1.tar.gz

Download URL jinja_wtforms-0.2.1.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
17458783c0dd7bcd947e34ccbebfee21cd58575a65dcb564076d222eb32cda14
BLAKE2b-256 checksum
How to use checksums
a1b0552747b3b49ebeaf14ca8202ea0b95566a8de93c1191c02e581c8e829a16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.0

Release files / jinja_wtforms-0.2.1-py3-none-any.whl

Download URL jinja_wtforms-0.2.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
758391041f6e27d72d99e265a0f2317f9e34affb92ebef5391122e37bb6adcaf
BLAKE2b-256 checksum
How to use checksums
2d8d546d764283d3ab1c58e5e5c4755c0b6e7c4dc9074e1f67cba842c4489902
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page