Skip to main content

tri.form is a form library

Project description

.. image::

.. image::


tri.form is alternative forms library for Django. It is inspired by, and comes from a frustration with, the standard Django forms.

Major features compared to Django forms:

- Supports :code:`__` syntax for going across table boundaries, similar to how Django does with QuerySets.
- Send in a callable that is late evaluated to determine if a field should be displayed (:code:`show`). This is very handy for showing a slightly different form to administrators for example.
- Easy configuration without writing entire classes that are only used in one place anyway.


You can either create a subclass of :code:`Form`...

.. code:: python

class UserForm(Form):
name = Field.text()
username = Field.text(is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_'))
is_admin = Field.boolean(
show=lambda form, field: form.request.user.is_staff, # show only for staff

def edit_user_view(request, username):
form = UserForm(request=request)

user = User.objects.get(username=username)
if form.is_valid() and request.method == 'POST':
return HttpRedirect('..')

return render(
context_instance=RequestContext(request, {'form': form}))

.. code:: html

<!-- edit_user.html -->
<form action="" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Save" />

or just instantiate a :code:`Form` with a :code:`Field` list and use it directly:

.. code:: python

def edit_user_view(request, username):
form = Form(fields=[
is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_')),
show=lambda form, field: form.request.user.is_staff, # show only for staff

# rest of view function...

You can also generate forms from Django models automatically (but still change the behavior!). The above example
is equivalent to:

.. code:: python

def edit_user_view(request, username):
form = Form.from_model(
# the field 'name' is generated automatically and we are fine with the defaults
username__is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_'),
is_admin__show=lambda form, field: form.request.user.is_staff) # show only for staff

# rest of view function...

or even better: use :code:`tri.form.views.create_or_edit_object`:

.. code:: python

def edit_user_view(request, username):
return create_or_edit_object(

form__username__is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_'),
form__is_admin__show=lambda form, field: form.request.user.is_staff) # show only for staff
# no html template! tri.form has a nice default for you :P

tri.form pre-packages sets of defaults for common field types as 'shortcuts'. Some examples include :code:`Field.boolean`,
:code:`Field.integer` and :code:`Field.choice`. The full list of shortcuts can be found in the `API documentation for Field <api.html#tri.form.Field>`_.

Running tests

You need tox installed then just `make test`.





1.11.0 (2016-02-29)

* Datetime fields used to not roundtrip cleanly via the form (they output
milliseconds then failed on parsing them) Field.file didn't exist.

* Changed syntax for specifying html attributes and classes. They are now use
the same way of addressing as other things, e.g.:
Field.choice(attrs__foo="bar", attrs__class__baz=True) will yield something
like `<select ... class="baz" foo=bar>...</select>`

1.10.0 (2016-02-08)

* Made sure form validation is only run once

* Fixed input form class and render context to create_or_edit_object view

1.9.0 (2016-01-15)

* Fixed default value initialization on Field attributes to not reuse containers.

* Added support for ManyToManyField when generating forms for model objects.

* Added 'read_from_instance' and 'write_to_instance' callbacks for customized instance marshalling.

1.8.0 (2016-01-13)

Bugfix release.

* Added missing 'after' attribute on Field prohibiting form order customization

* Fixed default value handling of 'attr' to make None a valid value when no attribute should be read.

* Fixed CSS handling on required fields.

1.7.0 (2016-01-13)

* Made evaluation of choices lazy even when there is a None alternative.

Project details

Download files

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

Files for tri.form, version 1.11.0
Filename, size File type Python version Upload date Hashes
Filename, size tri.form-1.11.0.tar.gz (16.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page