Skip to content

Commit

Permalink
Getting a layout example working in the test_app and then updating th…
Browse files Browse the repository at this point in the history
…e README to reflect what I have actually seen working
  • Loading branch information
pydanny committed Jul 19, 2009
1 parent 2fb0e8c commit 618c5b4
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 102 deletions.
102 changes: 57 additions & 45 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Using the django-uni-form filter (Easy and fun!)

Using the django-uni-form templatetag in your view (Intermediate)
====================================================================
1. In your form class add the following after field definitions::
1. In your views.py add the following after field definitions::

from django.shortcuts import render_to_response

Expand Down Expand Up @@ -103,53 +103,65 @@ Using the django-uni-form templatetag in your form class (Intermediate)
{% endwith %}


Adding a layout to your form (advanced)
=======================================
Adding a layout to your form class (advanced)
==============================================

Uniform helper can have a layout. A layout can consist of fieldsets, rows, columns, HTML and fields.
A complex Example::
Uniform helpers can use layout objects. A layout can consist of fieldsets, rows, columns, HTML and fields. A simple Example::

from uni_form.helpers import Layout, Fieldset, Column, Row, HTML
from django import forms

from uni_form.helpers import FormHelper, Submit, Reset
from uni_form.helpers import Layout, Fieldset, Row, HTML

help_text = render_to_string("example/help_text.html")
layout = Layout(Fieldset(_('Basic Settings'),
'title',
'type',
'available_date',
),
Fieldset(_('Overview'),
Column(Fieldset(_('Object address'),
Row('address', 'street_number'),
Row('zip', 'city'),
'area',
),
Fieldset(_("Next public transport"),
'train_station',
Row('tram_station','tram_number'),
Row('bus_station','bus_number'),
),
),
Column("is_for_rent",
Fieldset(_("Rent"),
'rent-price',
),
Fieldset(_("Sell"),
'buy_price',
),
Fieldset(_("Measurements"),
'floor_space',
'room_height',
'construction_year',
),
),
Fieldset(_('Additional Function'),
HTML('<p class="tip">%s</p>' % unicode(help_text)),
'features',
),
Fieldset(_("Description"),
"description")
)
helper.add_layout(layout)
class LayoutTestForm(forms.Form):

is_company = forms.CharField(label="company", required=False, widget=forms.CheckboxInput())
email = forms.CharField(label="email", max_length=30, required=True, widget=forms.TextInput())
password1 = forms.CharField(label="password", max_length=30, required=True, widget=forms.PasswordInput())
password2 = forms.CharField(label="re-enter password", max_length=30, required=True, widget=forms.PasswordInput())
first_name = forms.CharField(label="first name", max_length=30, required=True, widget=forms.TextInput())
last_name = forms.CharField(label="last name", max_length=30, required=True, widget=forms.TextInput())

# Attach a formHelper to your forms class.
helper = FormHelper()

# Create some HTML that you want in the page.
# Yes, in real life your CSS would be cached, but this is just a simple example.
style = """
<style>
.formRow {
color: red;
}
</style>

"""
# create the layout object
layout = Layout(
# first fieldset shows the company
Fieldset('', 'is_company'),

# second fieldset shows the contact info
Fieldset('Contact details',
HTML(style),
'email',
Row('password1','password2'),
'first_name',
'last_name',
)
)

helper.add_layout(layout)

submit = Submit('add','Add this contact')
helper.add_input(submit)

Then, just like in the previous example, add the following to your template::

{% load uni_form %}
{% with form.helper as helper %}
{% uni_form form helper %}
{% endwith %}


This allows you to group fields in fieldsets, or rows or columns or add HTML between fields etc.

Expand Down
102 changes: 57 additions & 45 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Using the django-uni-form filter (Easy and fun!)

Using the django-uni-form templatetag in your view (Intermediate)
====================================================================
1. In your form class add the following after field definitions::
1. In your views.py add the following after field definitions::

from django.shortcuts import render_to_response
Expand Down Expand Up @@ -103,53 +103,65 @@ Using the django-uni-form templatetag in your form class (Intermediate)
{% endwith %}


Adding a layout to your form (advanced)
=======================================
Adding a layout to your form class (advanced)
==============================================

Uniform helper can have a layout. A layout can consist of fieldsets, rows, columns, HTML and fields.
A complex Example::
Uniform helpers can use layout objects. A layout can consist of fieldsets, rows, columns, HTML and fields. A simple Example::

from uni_form.helpers import Layout, Fieldset, Column, Row, HTML
from django import forms
from uni_form.helpers import FormHelper, Submit, Reset
from uni_form.helpers import Layout, Fieldset, Row, HTML
help_text = render_to_string("example/help_text.html")
layout = Layout(Fieldset(_('Basic Settings'),
'title',
'type',
'available_date',
),
Fieldset(_('Overview'),
Column(Fieldset(_('Object address'),
Row('address', 'street_number'),
Row('zip', 'city'),
'area',
),
Fieldset(_("Next public transport"),
'train_station',
Row('tram_station','tram_number'),
Row('bus_station','bus_number'),
),
),
Column("is_for_rent",
Fieldset(_("Rent"),
'rent-price',
),
Fieldset(_("Sell"),
'buy_price',
),
Fieldset(_("Measurements"),
'floor_space',
'room_height',
'construction_year',
),
),
Fieldset(_('Additional Function'),
HTML('<p class="tip">%s</p>' % unicode(help_text)),
'features',
),
Fieldset(_("Description"),
"description")
)
helper.add_layout(layout)
class LayoutTestForm(forms.Form):

is_company = forms.CharField(label="company", required=False, widget=forms.CheckboxInput())
email = forms.CharField(label="email", max_length=30, required=True, widget=forms.TextInput())
password1 = forms.CharField(label="password", max_length=30, required=True, widget=forms.PasswordInput())
password2 = forms.CharField(label="re-enter password", max_length=30, required=True, widget=forms.PasswordInput())
first_name = forms.CharField(label="first name", max_length=30, required=True, widget=forms.TextInput())
last_name = forms.CharField(label="last name", max_length=30, required=True, widget=forms.TextInput())
# Attach a formHelper to your forms class.
helper = FormHelper()

# Create some HTML that you want in the page.
# Yes, in real life your CSS would be cached, but this is just a simple example.
style = """
<style>
.formRow {
color: red;
}
</style>
"""
# create the layout object
layout = Layout(
# first fieldset shows the company
Fieldset('', 'is_company'),
# second fieldset shows the contact info
Fieldset('Contact details',
HTML(style),
'email',
Row('password1','password2'),
'first_name',
'last_name',
)
)

helper.add_layout(layout)
submit = Submit('add','Add this contact')
helper.add_input(submit)
Then, just like in the previous example, add the following to your template::

{% load uni_form %}
{% with form.helper as helper %}
{% uni_form form helper %}
{% endwith %}

This allows you to group fields in fieldsets, or rows or columns or add HTML between fields etc.

Expand Down
109 changes: 100 additions & 9 deletions uni_form/tests/test_project/test_app/forms.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,120 @@
from django import forms
from django.template.loader import render_to_string

from uni_form.helpers import FormHelper, Submit, Reset

from uni_form.helpers import Layout, Fieldset, Column, Row, HTML


class TestForm(forms.Form):

character_field = forms.CharField(label="Character Field", max_length=30, required=True, widget=forms.TextInput())
url_field = forms.URLField(label='URL field', verify_exists=False, max_length=100, required=True, widget=forms.TextInput())
textarea_field = forms.CharField(label='textarea_field', required=True, widget=forms.Textarea())


character_field = forms.CharField(label="Character Field", help_text="I am help text", max_length=30, required=True, widget=forms.TextInput())
url_field = forms.URLField(label='URL field', verify_exists=False, max_length=100, required=True, widget=forms.TextInput())
textarea_field = forms.CharField(label='Textareafield', required=True, widget=forms.Textarea())
hidden_field = forms.CharField(label='textarea_field', required=True, widget=forms.HiddenInput())


class HelperTestForm(TestForm):

# Attach a formHelper to your forms class.
helper = FormHelper()

# Add in a class and id
helper.form_id = 'this-form-rocks'
helper.form_class = 'search'

# add in a submit and reset button
submit = Submit('search','search this site')
submit = Submit('enter','enter some data')
helper.add_input(submit)
reset = Reset('reset','reset button')
helper.add_input(reset)



class LayoutTestForm(forms.Form):

is_company = forms.CharField(label="company", required=False, widget=forms.CheckboxInput())
email = forms.CharField(label="email", max_length=30, required=True, widget=forms.TextInput())
password1 = forms.CharField(label="password", max_length=30, required=True, widget=forms.PasswordInput())
password2 = forms.CharField(label="re-enter password", max_length=30, required=True, widget=forms.PasswordInput())
first_name = forms.CharField(label="first name", max_length=30, required=True, widget=forms.TextInput())
last_name = forms.CharField(label="last name", max_length=30, required=True, widget=forms.TextInput())

# Attach a formHelper to your forms class.
helper = FormHelper()

# create some HTML that you want in the page
style = """
<style>
.formRow {
color: red;
}
</style>
"""
# create the layout object
layout = Layout(
# first fieldset shows the company
Fieldset('', 'is_company'),

# second fieldset shows the contact info
Fieldset('Contact details',
HTML(style),
'email',
Row('password1','password2'),
'first_name',
'last_name',
)
)

helper.add_layout(layout)

submit = Submit('add','Add this contact')
helper.add_input(submit)



class ComplexLayoutTest(forms.Form):
"""
TODO: get digi604 to make this work
help_text = render_to_string("example/help_text.html")
layout = Layout(Fieldset(_('Basic Settings'),
'title',
'type',
'available_date',
),
Fieldset(_('Overview'),
Column(Fieldset(_('Object address'),
Row('address', 'street_number'),
Row('zip', 'city'),
'area',
),
Fieldset(_("Next public transport"),
'train_station',
Row('tram_station','tram_number'),
Row('bus_station','bus_number'),
),
),
Column("is_for_rent",
Fieldset(_("Rent"),
'rent-price',
),
Fieldset(_("Sell"),
'buy_price',
),
Fieldset(_("Measurements"),
'floor_space',
'room_height',
'construction_year',
),
),
Fieldset(_('Additional Function'),
HTML('<p class="tip">%s</p>' % unicode(help_text)),
'features',
),
Fieldset(_("Description"),
"description")
)
helper.add_layout(layout)
"""
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<p>
<a href="{% url test_index %}">Basic test</a> |
<a href="{% url form_helper %}">Form Helper test</a> |
<a href="{% url view_helper %}">View Helper test</a>
<a href="{% url view_helper %}">View Helper test</a> |
<a href="{% url layout_test %}">Layout test</a>
</p>
{% block body %}
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "test_app/base.html" %}
{% load uni_form %}

{% block body%}
<h1>
Django Uni-Form view layout test
</h1>

{% with form.helper as helper %}
{% uni_form form helper %}
{% endwith %}


{% endblock %}
Loading

0 comments on commit 618c5b4

Please sign in to comment.