Skip to main content

Create wtforms from Jinja templates

Project description

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.csrf_token() }}
    <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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja_wtforms-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

jinja_wtforms-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file jinja_wtforms-0.1.0.tar.gz.

File metadata

  • Download URL: jinja_wtforms-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-40-generic

File hashes

Hashes for jinja_wtforms-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2fbb459e6e1d80526118847990a0090ef72dc816ed8d56515ddff7bfb3c98cc0
MD5 0f3ab4ffe4b59763a95ab5f1f16173a3
BLAKE2b-256 6eca17c01ae4164f38917b5b227f5c32504f74bc4e7a6d4e0f2cd4fe4b5be7d1

See more details on using hashes here.

File details

Details for the file jinja_wtforms-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jinja_wtforms-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-40-generic

File hashes

Hashes for jinja_wtforms-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 414fc527a6ce26d7c1ec37c5ff31015f825e55b7e207900ba3ac2d88dac67279
MD5 d39f471b923e8e89502d14712f81d607
BLAKE2b-256 3f5934a148efd5cc5ea1367355fa491b2822df7b366fd215472219241539da97

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page