forked from pydanny/django-uni-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.txt
176 lines (122 loc) · 5.99 KB
/
usage.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
===========================
Ways to use Django-uni-form
===========================
Using the django-uni-form filter (Easy and fun!)
=================================================
1. Add ``{% load uni_form %}`` to the template that calls your form.
2. Append your form call with the as_uni_form filter::
{{ my_form|as_uni_form }}
3. Add the class of 'uniForm' to your form. Example::
<form action="" method="post" class="uniForm">
4. Refresh and enjoy!
Using the django-uni-form templatetag in your view (Intermediate)
====================================================================
1. In your views.py add the following after field definitions::
from django.shortcuts import render_to_response
from uni_form.helpers import FormHelper, Submit, Reset
from my_project.forms.MyForm
def my_view(request):
# Create the form
form = MyForm()
# create a formHelper
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')
helper.add_input(submit)
reset = Reset('reset','reset button')
helper.add_input(reset)
# create the response dictionary
response_dictionary = {'form':form, 'helper': helper}
return render_to_response('my_template.html', response_dictionary)
2. In your template do the following::
{% load uni_form %}
{% uni_form form helper %}
Using the django-uni-form templatetag in your form class (Intermediate)
========================================================================
1. In your form class add the following after field definitions::
from uni_form.helpers import FormHelper, Submit, Reset
class MyForm(forms.Form):
title = forms.CharField(label=_("Title"), max_length=30, widget=forms.TextInput())
# 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')
helper.add_input(submit)
reset = Reset('reset','reset button')
helper.add_input(reset)
2. In your template do the following::
{% load uni_form %}
{% with form.helper as helper %}
{% uni_form form helper %}
{% endwith %}
Using the django-uni-form templatetag to change action/method (Intermediate)
============================================================================
1. In your form class add the following after field definitions::
from uni_form.helpers import FormHelper, Submit
class MyForm(forms.Form):
title = forms.CharField(label=_("Title"), max_length=30, widget=forms.TextInput())
# Attach a formHelper to your forms class.
helper = FormHelper()
# Change the form and method
helper.form_action = 'my-url-name-defined-in-url-conf'
helper.form_method = 'GET' # Only GET and POST are legal
# add in a submit and reset button
submit = Submit('search','search this site')
helper.add_input(submit)
2. In your template do the following::
{% load uni_form %}
{% with form.helper as helper %}
{% uni_form form helper %}
{% endwith %}
Adding a layout to your form class (Intermediate)
==================================================
Uniform helpers can use layout objects. A layout can consist of fieldsets, rows, columns, HTML and fields. A simple Example::
from django import forms
from uni_form.helpers import FormHelper, Submit, Reset
from uni_form.helpers import Layout, Fieldset, Row, HTML
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.