Search code examples
pythondjangodjango-allauthdjango-crispy-forms

All-auth / Crispyform : Error messages not displayed


I'm working on the all-auth authentification process of my website and I encounter a problem: I can't see the error messages of the login / signup views, when I do something wrong (bad password, not typing same password in signup for ex.) the page just reload.

Views.py

class CustomLogin(LoginView):
    """
    Custom login view from Allauth
    """
    def get_context_data(self, **kwargs):
        context = super(CustomLogin, self).get_context_data(**kwargs)
        context.update({'title': self.title, 'help_text': self.help_text})
        return context

    title = 'Connectez-vous'
    help_text = 'Veuillez rentrer votre login et votre mot de passe pour vous connecter.'

    form = CustomLoginForm
    template_name = "allauth-custom/allauth-card.html"

Forms.py

class CustomLoginForm(LoginForm):
    def __init__(self, *args, **kwargs):
        super(CustomLoginForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'signup_form'
        self.helper.form_class = 'signup'
        self.helper.form_method = 'post'
        self.helper.form_action = reverse('thankyou')
        # and then the rest as usual:
        self.helper.form_show_errors = True
        self.helper.form_show_labels = False
        self.helper.add_input(Submit('Log in', 'log in'))

Template

{% extends "base.html" %}
{% load socialaccount %}
{% load bootstrap3 %}
{% load crispy_forms_tags %}
{% load i18n %}

{% bootstrap_messages %}

{% block content %}
<section id='masthead'>
<div class="container ">
<div class="row justify-content-center">
    <div class="card col-6 ">
      <div class="card-body">
        <h4 class="card-title">{{title}}</h4>
         <p class="card-text">{{help_text}}</p>
         {% if form.errors %}
           <div class="alert alert-danger">
               {% for field in form %}
                 {% for error in field.errors %}
                         <strong>{{ error|escape }}</strong><br />
                 {% endfor %}
             {% endfor %}
           </div>
             {% for error in form.non_field_errors %}
                 <div class="alert alert-danger">
                     <strong>{{ error|escape }}</strong>
                 </div>
             {% endfor %}
         {% endif %}
         {% crispy form %}
      </div>
  </div>
</div>
</section>
{% endblock %}

Solution

  • You should pass self (the current instance of your form) when doing self.helper = FormHelper()

    self.helper = FormHelper(self)

    From the exemple on this page: http://django-crispy-forms.readthedocs.io/en/latest/form_helper.html

    from crispy_forms.helper import FormHelper
    
    class ExampleForm(forms.Form):
        def __init__(self, *args, **kwargs):
             super(ExampleForm, self).__init__(*args, **kwargs)
             self.helper = FormHelper(self) # You missed to pass the self argument here
    

    Also, you define your action url as self.helper.form_action = reverse('thankyou')

    I don't think this is what you want. The form_action should be your login url.