gpt4 book ai didi

Django all-auth Form 错误未显示

转载 作者:行者123 更新时间:2023-12-01 13:42:07 24 4
gpt4 key购买 nike

我正在使用 django-allauth,每当我在登录表单中输入错误的密码时,页面就会重新加载并且不显示任何错误。这是我的 html 代码:

    <form class="login" method="POST" action="{% url 'account_login' %}">
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<span><b> {{error}} </b><span>
</div>
{% endfor %}
{% endfor %}
{% endif %}
{% csrf_token %}

<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
{{ form.login }}
<label class="mdl-textfield__label" for="id_login">Username/Email:</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
{{ form.password }}
<label class="mdl-textfield__label" for="id_password">Password</label>
</div>

<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="id_remember">
{{form.remember}}<span class='mdl-checkbox__label 'align='left'>Remember me</span>
</label></br>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<a class="button secondaryAction" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a><br/></br>
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--raised mdl-button--colored" type="submit">{% trans "Sign In" %}</button>
<br/>
<br/>
</form>

如果信息输入正确,表单正常显示并正常提交。对于注册表单,错误消息显示,但它给出了一个奇怪的消息,如 '%(model_name)s 与这个 %(field_label )s 已经存在。' 如果我尝试输入一个已经存在但对于用户名的电子邮件,它会给出一个正常的 具有该用户名的用户已经存在。

这是signup.html

<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<span><b> {{error}} </b><span>
</div>
{% endfor %}
{% endfor %}
{% endif %}
{% csrf_token %}
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
{{ form.username }}
<label class="mdl-textfield__label" for="id_username">Username</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
{{form.email}}
<label class="mdl-textfield__label" for="id_email">Email</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
{{form.password1}}
<label class="mdl-textfield__label" for="id_password1">Password</label>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
{{form.password2}}
<label class="mdl-textfield__label" for="id_password2">Password(again)</label>
</div>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}<br/>

<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--raised mdl-button--colored" type="submit">{% trans "Sign Up" %}</button>
</form>

注册 View

class SignupView(RedirectAuthenticatedUserMixin, CloseableSignupMixin,
AjaxCapableProcessFormViewMixin, FormView):
template_name = "account/signup." + app_settings.TEMPLATE_EXTENSION
form_class = SignupForm
redirect_field_name = "next"
success_url = None

@sensitive_post_parameters_m
def dispatch(self, request, *args, **kwargs):
return super(SignupView, self).dispatch(request, *args, **kwargs)

def get_form_class(self):
return get_form_class(app_settings.FORMS, 'signup', self.form_class)

def get_success_url(self):
# Explicitly passed ?next= URL takes precedence
ret = (
get_next_redirect_url(
self.request,
self.redirect_field_name) or self.success_url)
return ret

def form_valid(self, form):
# By assigning the User to a property on the view, we allow subclasses
# of SignupView to access the newly created User instance
self.user = form.save(self.request)
return complete_signup(self.request, self.user,
app_settings.EMAIL_VERIFICATION,
self.get_success_url())

def get_context_data(self, **kwargs):
ret = super(SignupView, self).get_context_data(**kwargs)
form = ret['form']
email = self.request.session.get('account_verified_email')
if app_settings.SIGNUP_EMAIL_ENTER_TWICE:
email_keys = ['email1', 'email2']
else:
email_keys = ['email']
for email_key in email_keys:
form.fields[email_key].initial = email
login_url = passthrough_next_redirect_url(self.request,
reverse("account_login"),
self.redirect_field_name)
redirect_field_name = self.redirect_field_name
redirect_field_value = get_request_param(self.request,
redirect_field_name)
ret.update({"login_url": login_url,
"redirect_field_name": redirect_field_name,
"redirect_field_value": redirect_field_value})
return ret

signup = SignupView.as_view()

登录查看

class LoginView(RedirectAuthenticatedUserMixin,
AjaxCapableProcessFormViewMixin,
FormView):
form_class = LoginForm
template_name = "account/login." + app_settings.TEMPLATE_EXTENSION
success_url = None
redirect_field_name = "next"

@sensitive_post_parameters_m
def dispatch(self, request, *args, **kwargs):
return super(LoginView, self).dispatch(request, *args, **kwargs)

def get_form_kwargs(self):
kwargs = super(LoginView, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs

def get_form_class(self):
return get_form_class(app_settings.FORMS, 'login', self.form_class)

def form_valid(self, form):
success_url = self.get_success_url()
try:
return form.login(self.request, redirect_url=success_url)
except ImmediateHttpResponse as e:
return e.response

def get_success_url(self):
# Explicitly passed ?next= URL takes precedence
ret = (get_next_redirect_url(
self.request,
self.redirect_field_name) or self.success_url)
return ret

def get_context_data(self, **kwargs):
ret = super(LoginView, self).get_context_data(**kwargs)
signup_url = passthrough_next_redirect_url(self.request,
reverse("account_signup"),
self.redirect_field_name)
redirect_field_value = get_request_param(self.request,
self.redirect_field_name)
site = get_current_site(self.request)

ret.update({"signup_url": signup_url,
"site": site,
"redirect_field_name": self.redirect_field_name,
"redirect_field_value": redirect_field_value})
return ret

login = LoginView.as_view()

最佳答案

如果还有问题,那么你可以使用这个

{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}

关于Django all-auth Form 错误未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39183479/

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