bryan.blackbee
bryan.blackbee

Reputation: 1954

'tuple' object has no attribute 'fields' using Django Crispy Forms

Given a form, I want to wrap some fields in Div using django-crispy-forms.

class SignupForm(forms.Form):
  def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_method = 'POST'
    self.helper.form_action = ''
    self.helper.add_input(Submit('submit', 'Create Account', css_class="btn-success"))
    self.helper.layout = Layout(
        Div(
            'username',
            'password',
            css_class="col-md-6"
        ),
        Div(
            'name',
            'age',
            css_class="col-md-4"
        ),
    ),
  username=forms.CharField(max_length=128)
  password=forms.CharField(max_length=128, widget=forms.PasswordInput())
  name=forms.CharField(max_length=128)
  age=forms.IntegerField(required=False)

However, in the view, when I use {% crispy form %}, it is throwing me the error 'tuple' object has no attribute 'fields'.

I'm suspecting that my Layout was not instantiated correctly but cannot move from here.

Upvotes: 1

Views: 1247

Answers (1)

bryan.blackbee
bryan.blackbee

Reputation: 1954

Solved it myself. The solution to this was that there was an extra comma at the end of Layout. The correct solution is:

self.helper.layout = Layout(
    Div(
        'username',
        'password',
        css_class="col-md-6"
    ),
    Div(
        'name',
        'age',
        css_class="col-md-4"
    ),
)

By removing the comma a the last line.

Upvotes: 2

Related Questions