Skip to main content
# Purpose of Field Tree

This plugin is a wrapper for dynamic form rendering, and works even for users
that block scripts (gasp! I know!).

The most common form of the "I really need JavaScript for this" attitude is
dynamic form rendering, where one select box renders another select box and so
on. One example you may have encountered is the obligitory Country-State-City
select box.

This plugin makes it easy to render a form based on input. Plus, it works
without JavaScript! The plugin will automatically detect if JasvaScript is
turned off, and render a submit button for continuing.

Consequently, the form is resubmitted whether someone is using Javascript or no
Javascript. **Any developer using this plugin must plan accordingly.**

# Requirements

Since this is a Django plugin, obviously django must be installed. Besides that,
you must have jquery installed and linked within the page that uses FieldTree
in order to use the javascript functionality.

# Getting Started


1. You can install moreforms by typing

$ pip install django-moreforms

in the command line.

2. Ensure that 'moreforms' is listed in INSTALLED_APPS in `settings.py`:

INSTALLED_APPS = (
...
'moreforms',
...
)

3. Finally, to construct the FieldTree, do the following:

from django import forms
from moreforms import FieldTree as T, FieldLeaf as L

class MyForm(forms.Form):
name = forms.CharField(...)
age = forms.IntegerField(...)
...

# Every form with a FieldTree must have a delete_field method. I'll
# explain later...
def delete_field(self, name):
del self.field[name]

# Make sure to override the __init__ method.
def __init__(self, *args, **kwargs):

request = kwargs.pop('request', None) # Don't add request as an
# argument itself.

super(MyForm, self).__init__(*args, **kwargs)

tree = T(request, self,
L(None, 'name', children=(
L('.*', 'age'),
))
)

tree.vacuum()

4. Within the django template you need to load the template tags:

{% load moreforms %}

{{ form.as_p }} {% auto_submit %}

`auto_submit` will render both the javascript and the non-script portions
of the FieldTree.

## What the code does.

`FieldTree` is, essentially a tree, where each leaf has a regular expression,
a name of a field, and (optionally) any children, which--in themselves--are
leaves.

The **regular expression** is what is expected from the parent's field. In
the above example, we want to render the 'age' field if the value of
'name' is posted. Note that the top-most leaf ('name') has `None` as the
regexp. This is becaue **the top-most leaf must have a None regexp.**

## That's it!

Now, just create the form in the view, taking care to pass in `request` as well.

def my_view(request):

form = MyForm(request)
return render(request, 'some_page.html', {'form' : form})

# A Few Notes

## `delete_field` function.

Since a FieldTree cannot delete the fields on the passed-in form directly,
they must be deleted manually from the form. Basically you just need the lines
provided in the example.

## Default input value

If you want a default input value (e.g. automatically fill in the submitted
`name`), do the following in the form:

class MyForm(forms.Form):
...
def __init__(self, *args, **kwargs):
...
super(...).__init__(...)
...
self.initial = {'name' : ... }

This is standard for django forms, but just thought you'd like to know.

## You might want a note...

You might want a note preceeding the form to explain how the TreeField works.

Download files

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

Source Distribution

django-moreforms-1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django-moreforms-1.0.linux-x86_64.tar.gz (12.3 kB view details)

Uploaded Source

File details

Details for the file django-moreforms-1.0.tar.gz.

File metadata

File hashes

Hashes for django-moreforms-1.0.tar.gz
Algorithm Hash digest
SHA256 d7784df309bc982a18f949053cfda97965315d5bd667be3b25218f3fe029ce0e
MD5 ab235df853afb0569cdf79083cd3cd80
BLAKE2b-256 dd60e16f2d95b4b900622335e231d8e9abe2d2e1faf103512a0ecb697e5b0eb3

See more details on using hashes here.

File details

Details for the file django-moreforms-1.0.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for django-moreforms-1.0.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 339d67da599635fe0ddde5f9e44a4a293a54ba98f79b0cf5fcbca46615457169
MD5 cc94a705531247e5bb8319ff4ffe3d92
BLAKE2b-256 61d41b60b856c0e8d08429ca5db0003de37a841b74824f509a29e338e6f3809f

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2

1 file

1.1

2 files

This release

1.0 This release

2 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