gpt4 book ai didi

python - Allauth/脆皮表格 : Error messages not displayed

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:56 29 4
gpt4 key购买 nike

我正在对我的网站进行全身份验证过程,但遇到了一个问题:当我做错事时(密码错误,没有输入相同的内容),我看不到登录/注册 View 的错误消息例如注册时的密码。)页面只需重新加载。

View .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"

表单.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

此外,您还可以将操作网址定义为 self.helper.form_action = reverse('thankyou')

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

关于python - Allauth/脆皮表格 : Error messages not displayed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47310496/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com