All-auth / Crispyform:不显示错误信息

All-auth / Crispyform : Error messages not displayed

我正在处理我网站的全授权认证过程,我遇到了一个问题:当我做错事时,我看不到登录/注册视图的错误消息(密码错误,不是在注册时输入相同的密码。)页面刚刚重新加载。

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'))

模板

{% 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 %}

您应该在执行 self.helper = FormHelper()

时传递 self(表单的当前实例)

self.helper = FormHelper(self)

来自本页的示例: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

此外,您将操作 url 定义为 self.helper.form_action = reverse('thankyou')

我认为这不是您想要的。 form_action 应该是您的登录名 url。