Search code examples
pythonhtmldjangodjango-crispy-forms

Failing to understand how inject crisp_form tags in django template


I am working on understanding Django crispy forms however I do not know if the mistake that I am making is on my forms.py or is on my login.html whereby I am not placing in the crispy form tags properly I get the following error when I load the html page.

AttributeError at /login/

'tuple' object has no attribute 'fields'

Code Below: forms.py

from django import forms
from phonenumber_field.modelfields import PhoneNumberField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit

class UserAccounts(forms.Form):

    name = forms.CharField(label='First Name', max_length=100)
    last_name = forms.CharField(label='Surname', max_length=100)
    phone_number = PhoneNumberField()
    email = forms.EmailField()
    password = forms.CharField(widget=forms.PasswordInput)
    verify_password = forms.CharField(widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.helper = FormHelper
        self.helper.form_method = 'POST'

        self.helper.layout = (

            'name', 
            'last_name',
            'phone_number',
            'email', 
            'password', 
            'verify_password',
            Submit('submit', 'Submit'),
        )

Code Below: login.html

<!DOCTYPE html>
{% load crispy_forms_tags %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
</head>
<body>

    {% crispy form form.helper %}


</body>
</html>

Solution

  • you have to load the crispy_forms_tags and after that you can easily use the crispy form.

    {% load crispy_forms_tags %}
    ...
    {{ form |crispy}}
    ...