Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
Taking Nagyv's idea for add_form_tag attribute on FormHelper and upda…
Browse files Browse the repository at this point in the history
…ting it to match changes implemented in django-uni-form 0.7.
  • Loading branch information
pydanny committed Apr 10, 2010
1 parent d758a38 commit 0722cbc
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -7,6 +7,7 @@ For 0.8.0
* Improved internal documentation
* form methods generated by FormHelper are in lowercase (http://github.com/pydanny/django-uni-form/issues#issue/20)
* uni_form.helpers.formHelper or None object must be passed into uni_form tag or a TypeError is thrown.
* Thanks to Nagy Viktor added add_form_tag attribute to FormHelper. Now you can use the uni_form tag without the leading and training form tags.

For 0.7.0

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -16,6 +16,7 @@ Karl Bowden <agentk>
Marcin Grzybowski <marcin.grzybowski@gildia.info>
Michael Lind Mortensen <illio>
mvaerle
Nagy Viktor
Patrick Lauber <digi604>
Skylar Saveland <skyl>

1 change: 1 addition & 0 deletions test_project/test_app/templates/test_app/base.html
Expand Up @@ -32,6 +32,7 @@
<a href="{% url view_helper %}">View Helper test</a> |
<a href="{% url layout_test %}">Layout test</a> |
<a href="{% url set_action_test %}">Set action test</a>
<a href="{% url lacking_form_tag %}">Lacking form tag</a>
</p>
{% block body %}
{% endblock %}
Expand Down
14 changes: 14 additions & 0 deletions test_project/test_app/templates/test_app/lacking_form_tag.html
@@ -0,0 +1,14 @@
{% extends "test_app/base.html" %}
{% load uni_form_tags %}

{% block body%}
<h1>
Django Uni-Form Lacking Form Tag Test
</h1>

{% uni_form form helper %}

{% endblock %}



1 change: 1 addition & 0 deletions test_project/test_app/urls.py
Expand Up @@ -8,6 +8,7 @@
url(r'^form_helper/$', "test_app.views.form_helper", name='form_helper'),
url(r'^layout_test/$', "test_app.views.layout_test", name='layout_test'),
url(r'^view_helper_set_action/$', "test_app.views.view_helper_set_action", name='set_action_test'),
url(r'^lacking_form_tag/$', "test_app.views.lacking_form_tag", name='lacking_form_tag'),


)
22 changes: 20 additions & 2 deletions test_project/test_app/views.py
Expand Up @@ -67,8 +67,6 @@ def view_helper_set_action(request):
return render_to_response('test_app/view_helper.html',
response_dictionary,
context_instance=RequestContext(request))



def form_helper(request):
if request.method == "POST":
Expand All @@ -89,4 +87,24 @@ def layout_test(request):
return render_to_response('test_app/form_helper.html', {
'form': form
}, context_instance=RequestContext(request))

def lacking_form_tag(request):
# Create the form
if request.method == "POST":
form = TestForm(request.POST)
else:
form = TestForm()

# create a formHelper
helper = FormHelper()

# remove the form tag
helper.add_form_tag = False

# create the response dictionary
response_dictionary = {'form':form, 'helper': helper}

return render_to_response('test_app/lacking_form_tag.html',
response_dictionary,
context_instance=RequestContext(request))

106 changes: 55 additions & 51 deletions uni_form/helpers.py
Expand Up @@ -22,11 +22,11 @@ class FormHelpersException(Exception):
class Submit(BaseInput):
"""
Used to create a Submit button descriptor for the uni_form template tag:
submit = Submit('Search the Site','search this site')
Note: The first argument is also slugified and turned into the id for the submit button.
submit = Submit('Search the Site','search this site')
Note: The first argument is also slugified and turned into the id for the submit button.
"""

input_type = 'submit'
Expand All @@ -36,10 +36,10 @@ class Submit(BaseInput):
class Button(BaseInput):
"""
Used to create a Submit input descriptor for the uni_form template tag:
button = Button('Button 1','Press Me!')
Note: The first argument is also slugified and turned into the id for the button.
button = Button('Button 1','Press Me!')
Note: The first argument is also slugified and turned into the id for the button.
"""

input_type = 'button'
Expand All @@ -56,10 +56,10 @@ class Hidden(BaseInput):
class Reset(BaseInput):
"""
Used to create a Hidden input descriptor for the uni_form template tag.
reset = Reset('Reset This Form','Revert Me!')
Note: The first argument is also slugified and turned into the id for the reset.
reset = Reset('Reset This Form','Revert Me!')
Note: The first argument is also slugified and turned into the id for the reset.
"""

Expand Down Expand Up @@ -191,44 +191,46 @@ class FormHelper(object):
By setting attributes to me you can easily create the text that goes
into the uni_form template tag. One use case is to add to your form
class.
Special attribute behavior:
method: Defaults to POST but you can also do 'GET'
form_action: applied to the form action attribute. Can be a named url in
your urlconf that can be executed via the *url* default template tag or can
simply point to another URL.
id: Generates a form id for dom identification.
If no id provided then no id attribute is created on the form.
class: add space seperated classes to the class list.
Defaults to uniForm.
Always starts with uniForm even do specify classes.
Demonstration:
First we create a MyForm class and instantiate it
>>> from django import forms
>>> from uni_form.helpers import FormHelper, Submit, Reset
>>> from django.utils.translation import ugettext_lazy as _
>>> class MyForm(forms.Form):
... title = forms.CharField(label=_("Title"), max_length=30, widget=forms.TextInput())
... # this displays how to attach a formHelper to your forms class.
... helper = FormHelper()
... helper.form_id = 'this-form-rocks'
... helper.form_class = 'search'
... submit = Submit('search','search this site')
... helper.add_input(submit)
... reset = Reset('reset','reset button')
... helper.add_input(reset)
After this in the template:
{% load uni_form_tags %}
{% uni_form form form.helper %}
Special attribute behavior:
method: Defaults to POST but you can also do 'GET'
form_action: applied to the form action attribute. Can be a named url in
your urlconf that can be executed via the *url* default template tag or can
simply point to another URL.
id: Generates a form id for dom identification.
If no id provided then no id attribute is created on the form.
class: add space seperated classes to the class list.
Defaults to uniForm.
Always starts with uniForm even do specify classes.
add_form_tag: Defaults to True. If set to False it renders the form without the form tags.
Demonstration:
First we create a MyForm class and instantiate it
>>> from django import forms
>>> from uni_form.helpers import FormHelper, Submit, Reset
>>> from django.utils.translation import ugettext_lazy as _
>>> class MyForm(forms.Form):
... title = forms.CharField(label=_("Title"), max_length=30, widget=forms.TextInput())
... # this displays how to attach a formHelper to your forms class.
... helper = FormHelper()
... helper.form_id = 'this-form-rocks'
... helper.form_class = 'search'
... submit = Submit('search','search this site')
... helper.add_input(submit)
... reset = Reset('reset','reset button')
... helper.add_input(reset)
After this in the template:
{% load uni_form_tags %}
{% uni_form form form.helper %}
"""
Expand All @@ -241,6 +243,7 @@ def __init__(self):
self.inputs = []
self.toggle = Toggle()
self.layout = None
self.add_form_tag = True

def get_form_method(self):
return self._form_method
Expand Down Expand Up @@ -279,6 +282,7 @@ def render_layout(self, form):
def get_attr(self):
items = {}
items['form_method'] = self.form_method.strip()
items['add_form_tag'] = self.add_form_tag

if self.form_action:
items['form_action'] = self.form_action.strip()
Expand Down
4 changes: 2 additions & 2 deletions uni_form/templates/uni_form/whole_uni_form.html
@@ -1,6 +1,6 @@
{% load uni_form_tags i18n %}

<form action="{{ form_action }}" class="uniForm {{ form_class }}" method="{{ form_method }}"{% if form_id %} id="{{ form_id|slugify }}"{% endif %}{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% if form_tag %}<form action="{{ form_action }}" class="uniForm {{ form_class }}" method="{{ form_method }}"{% if form_id %} id="{{ form_id|slugify }}"{% endif %}{% if form.is_multipart %} enctype="multipart/form-data"{% endif %}>{% endif %}
{% if form_html %}{{ form_html }}{% else %}
<fieldset class="inlineLabels">
<legend>* {% trans "Required fields" %}</legend>
Expand All @@ -21,4 +21,4 @@
{% endfor %}
</div>
{% endif %}
</form>
{% if form_tag %}</form>{% endif %}
2 changes: 2 additions & 0 deletions uni_form/templatetags/uni_form_tags.py
Expand Up @@ -89,6 +89,7 @@ def get_render(self, context):
inputs = []
toggle_fields = set(())
if attrs:
form_tag = attrs.get("add_form_tag", True)
form_method = attrs.get("form_method", 'POST')
form_action = attrs.get("form_action", '')
form_class = attrs.get("class", '')
Expand All @@ -111,6 +112,7 @@ def get_render(self, context):
'form_html':form_html,
'form_action':form_action,
'form_method':form_method,
'form_tag': form_tag,
'attrs':attrs,
'form_class' : form_class,
'form_id' : form_id,
Expand Down

0 comments on commit 0722cbc

Please sign in to comment.