domingo, 13 de fevereiro de 2011

Django Better Form

System - Sistema:
Ubuntu 10.04 LTS - the Lucid Lynx
Python 2.6.5
Django 1.2.4 final

Installing and configuring django-form-utils

 $ sudo apt-get install python-setuptools  
 $ sudo easy_install django-form-utils

Including form_utils:
.../settings.py
 
 INSTALLED_APPS = (  
   'django.contrib.auth',  
   'django.contrib.contenttypes',  
   'django.contrib.sessions',  
   'django.contrib.sites',  
   'django.contrib.messages',  
   'django.contrib.admin',  
   ...  
   'form_utils',  
 )  

Now change:
.../forms.py

 #from django import forms  
 from form_utils import forms  
 ...  


 class fooModelForm(forms.BetterModelForm):  
   class Meta:  
     model = fooModel  
     exclude = ('foo_field',)  
     fieldsets = [('Personal data', {'fields': ['name', 'email', 'phone'],  
                      #'description': 'Information',  
                      'classes': ['Personal data']  
                     }),]  
     row_attrs = {'name': {'class': 'style_names'},  
            'email': {'class': 'style_email'},  
            'phone': {'class': 'style_phone'},}  
and:
.../template/form.html
 <form action="." method="POST">{% csrf_token %}  
   {% if form.non_field_errors %}{{ form.non_field_errors }}{% endif %}  
   {% for fieldset in form.fieldsets %}  
     <fieldset class="{{ fieldset.classes }}">  
       {% if fieldset.legend %}  
         <legend>{{ fieldset.legend }}</legend>  
       {% endif %}  
       {% if fieldset.description %}  
         <p class="description">{{ fieldset.description }}</p>  
       {% endif %}  
       <ul>  
         {% for field in fieldset %}  
           {% if field.is_hidden %}  
             {{ field }}  
           {% else %}  
             <li{{ field.row_attrs }}>  
               {{ field.errors }}  
               {{ field.label_tag }}  
               {{ field }}  
             </li>     
           {% endif %}  
         {% endfor %}  
       </ul>  
     </fieldset>  
   {% endfor %}  
   <input type="submit" value="Enviar!" class="button">  
 </form>  

More information: https://bitbucket.org/carljm/django-form-utils/src/
Also: http://djangosnippets.org/snippets/1314/

Nenhum comentário:

Postar um comentário