【问题标题】:All-auth / Crispyform : Error messages not displayedAll-auth / Crispyform:不显示错误消息
【发布时间】:2017-11-15 14:52:51
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    在执行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
    

    另外,您将您的操作网址定义为self.helper.form_action = reverse('thankyou')

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

    【讨论】:

    • 我更改了设置但我没有解决我的问题:(
    • 我刚刚看到你定义了self.helper.form_action = reverse('thankyou') 这个url应该是你登录视图的url,而不是重定向用户的url...
    猜你喜欢
    • 2017-01-04
    • 2016-04-17
    • 2018-03-25
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2014-01-01
    • 2010-12-12
    相关资源
    最近更新 更多