Form generator framework

22 views
Skip to first unread message

Mikko Ohtamaa

unread,
Sep 22, 2006, 4:41:35 AM9/22/06
to Django developers
Hi,

I have created a little HTML form generator framework for Django. The
framework (django.contrib package) and an example application are
available at http://ohtis.net/formgenerator.tar.gz

I hope this would become (un)official part of Django distribution.
Please give some feedback.

Below some highlights and example application readme

Form Generator example
----------------------

formgenerator automatizes generating HTML form code

Form generator combines

- Django data model

- Form wrapper

- Customizable form field templates

and generates HTML form code automatically based on this information
(as an alternative typing in HTML form pages manually). You don't need
to repeat yourself by recreating and retyping field labels etc. things
again in several places: HTML code, form handling code and model code.

- formgenerator allows free mix and match of automatic generated form
fields
and manually placed form fields.

- One can customize automatic form generation by providing custom per
field
templates

- Layout changes are easier when you need to make change in one place
instead of
copy pasting the changed code for every HTML field

- formgenerator is clean, elegant, Python code

Example code
------------

Here is an example snipped (full example available in examples folder)
how to use Form generator.

from django.contrib.formgenerator import FormGenerator,
FieldDescriptor,
get_validation_errors

# Local imports
from models import ExampleUser, ExampleUserManager, crypt_password

...

#
# The registration form is divided to two parts.
# We use one form generator for each part.
#
# Label and description can be overridden locally,
# or fetched from model data.
#
# This is were we skip truckloads of manual HTML coding.
#

# Basic account information
account_info_generator = FormGenerator(
# Which fields are generated in this piece of HTML code
fields = [
FieldDescriptor("domain", label="Domain name",
description="Only letter
A-Z and digits allowed"),
FieldDescriptor("password"),
FieldDescriptor("password2", label="Password again"),
],
model=ExampleUser,
)

# Person's contact information
contact_info_generator = FormGenerator(
# Which fields are generated in this piece of HTML code
fields = [
FieldDescriptor("first_name"),
FieldDescriptor("last_name"),
FieldDescriptor("street_address"),
FieldDescriptor("city"),
FieldDescriptor("country"),
FieldDescriptor("email"),
],
model=ExampleUser,
)

...

# Create the FormWrapper, template, context, response.
form = forms.FormWrapper(manipulator, new_data, errors)

# Generate HTML form body code (fields) automatically
account_info_body = account_info_generator.generate(request, form)
contact_info_body = contact_info_generator.generate(request, form)

# Variables available in the template
params = {
'form': form,
'manipulator' : manipulator,
'account_info_body': account_info_body,
'contact_info_body': contact_info_body,
}

return render_to_response('register_form.html', params)

and then the HTML code:

<body>

<h1>Register an example user</h1>

<p>Demostratres Django formgenerator</p>

{% if form.has_errors %}
<h2>Please correct the following error{{ form.error_dict|pluralize
}}:</h2>
{% endif %}

<form method="post" action=".">

<h2>Account info</h2>
{{ account_info_body }}

<h2>Contact info</h2>
{{ contact_info_body }}

<br>
<input type="submit" />
</form>

</body>

Manuel Saelices

unread,
Sep 23, 2006, 1:45:24 PM9/23/06
to django-d...@googlegroups.com
Impressive... Apps like that is a perfect DRY example. It could be a
first step to develop a huge CMS based on django.
Reply all
Reply to author
Forward
0 new messages