StrictButton at the bottom of a modelforms

212 Views Asked by At

As I find borring to repeat myself, I tought of using the Meta.fields in my helper instead of copying twice my list of fields, just to have the StrictButton positioned at the bottom of my form. Here is my form:

class ContactForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
    super(ContactForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_class = 'form-horizontal'
    self.helper.label_class = "col-lg-2"
    self.helper.field_class = 'col-lg-8'
    self.helper.layout = Layout(
        self.Meta.fields + (
        StrictButton('Send', css_class='btn-default', type='submit'),
    )
    )
    self.helper.form_method = 'post'
    self.helper.form_action = ''

    class Meta:
        model = Contact
        fields = ('title', 'name', 'firstname', 'address', 'mail', 'tel',
              'mobile', 'login', 'password', 'note')

But this line

$ (self.Meta.fields + (StrictButton('Send', css_class='btn-default', type='submit',))

just doesn't work. It raises

Could not resolve form field '('title', 'name', 'firstname', 'address', 'mail', 'telephone', 'mobile', 'login', 'password', 'note', )'.

Do I really have to copy my fields again in order to have the StrictButton down my form?

1

There are 1 best solutions below

0
On BEST ANSWER

Thanks to madzohan, I needed to unpack Meta.fields. What works is:

   self.helper.layout = Layout(
        *(self.Meta.fields + (StrictButton('Send', css_class='btn-default', type='submit'),))
    )