# -*- coding:utf-8 -*- # # Use the tag like this: # # {% draw_form form fieldsets %} # # Where 'form' is the form to be draw and 'fieldsets' is a tuple containing the # fieldsets and the contained fields. # # Example on how to build the fieldsets parameter # # fiedsets = ( # ('Personal Data', {'fields':('name','gender'), 'id':'personal_data'}), # ('Address', {'fields':('street','number','city','zip_code'), 'id':'address'}), # ) # from django.template import Library register = Library() @register.simple_tag def draw_form(form, fieldsets=False): def get_fields_html(fields, f): fields_html = [] append = fields_html.append for field_name in fields: field = f[field_name] cls = [] help_text = '' errors = '' if f.fields[field_name].required: cls.append('required') if field.help_text: help_text = '%s' % field.help_text if f[field_name].errors: errors = str(f[field_name].errors) cls.append('error') cls = ' class="%s"' % " ".join(cls) append('