All-auth / Crispyform:未显示错误消息

时间:2017-11-15 14:52:51

标签: python django django-allauth django-crispy-forms

我正在研究我的网站的全身份验证过程,我遇到了一个问题:当我做错了什么时,我看不到登录/注册视图的错误消息(密码错误,不能输入相同内容)注册时的密码,例如,页面只是重新加载。

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

1 个答案:

答案 0 :(得分:1)

执行H_Cost

时,您应该传递self(表单的当前实例)

self.helper = FormHelper()

从此页面上的示例:http://django-crispy-forms.readthedocs.io/en/latest/form_helper.html

self.helper = FormHelper(self)

另外,您将操作网址定义为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

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