Skip to content

This is a fork of pydanni/django-uni-form in order to add a templatetag that supports formwizards.

License

Notifications You must be signed in to change notification settings

nagyv/django-uni-form

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

98 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-uni-form (Django Uni-Form)

Django forms are easily rendered as tables, paragraphs, and unordered lists. However, elegantly rendered div based forms is something you have to do by hand. The purpose of this application is to provide a simple tag and/or filter that lets you quickly render forms in a div format.

Uni-form has been selected as the base model for the design of the forms.

Installing django-uni-form

  1. Install as uni_form in your Django apps directory.
  2. Copy the site_media files in uni_form to your project site_media directory.

    • uni-form-generic.css
    • uni-form.css
    • uni-form.jquery.js
  3. Add 'uni_form' to INSTALLED_APPS in settings.py.

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.

About

This is a fork of pydanni/django-uni-form in order to add a templatetag that supports formwizards.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%