maraujop/django-crispy-forms — Issue #14: No FormMixin
Author: Christophe31
Created: 2012-02-23 08:33:44+00:00
State: open

Some of us love when there is a bit more magic.

I use a mixin class in all of my projects witch use uni_form or crispy_forms.

My MixIn class just add a simple property, here it is:

```Python

@property
def helper(self):
helper = helpers.FormHelper()

if hasattr(self, "parent_url"):
back = helpers.Button('back', 'Retour')
back.field_classes += ' primaryAction'
helper.add_input(back)

reset = helpers.Reset('reset', 'Défaire')
reset.field_classes += ' primaryAction'
helper.add_input(reset)

submit = helpers.Submit('submit', 'Valider')
submit.field_classes += ' primaryAction'
helper.add_input(submit)

helper.form_method = getattr(self, "form_method", 'POST')

if hasattr(self, "action_url"):
helper.form_action = self.action_url

if hasattr(self, "form_style"):
helper.form_style = self.form_style
if not hasattr(self, "form_layout"):
helper.add_layout(helpers.Layout(helpers.Fieldset("",*self.fields.keys())))
if hasattr(self, "form_layout"):
fs=[]
for name, fields in self.form_layout.iteritems():
fs.append(helpers.Fieldset(name, *fields))
helper.add_layout(helpers.Layout(*fs))

return helper

````