在 Django 中使用内置重置密码和密码更改时如何显示错误消息
how can i display error messages when using the inbuilt reset password and password change in Django
我正在处理一些用户忘记密码以及使用 Django 内置功能重置密码的问题,但我正在使用我的模板来呈现页面和表单。我想要一种方法来呈现错误消息或成功消息,以便用户了解在密码太短的情况下发生了什么。
urls.py
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.Dashboard, name='dashboard'),
path('login/', views.Login, name='login'),
path('logout/', views.Logout, name='logout'),
path('register/', views.Register, name='register'),
path('forgetpassword/', views.ForgetPassword, name='forgetpassword'),
path('password_change/', auth_views.PasswordChangeView.as_view(
template_name='auth/password/change-password.html'), name='password_change'),
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(
template_name='auth/password/change-password-done.html'), name='password_change_done'),
变化-password.html
{% extends 'auth/base.html' %}
{% load static %}
{% block content %}
<div class="page-wrapper" style="height: 100vh!important;">
<div class="page-content--bge5">
<div class="container">
<div class="login-wrap">
<div class="login-content">
<div class="login-logo">
<a href="#">
<img src="{% static 'images/icon/logo-new.png' %}" alt="CoolAdmin">
</a>
</div>
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{message.tags}} with-close alert-dismissible fade show">
{{message}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% endif %}
<div class="login-form">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>Current Password</label>
<input class="au-input au-input--full" type="password" name="old_password" id="id_old_password" placeholder="Current Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password1" id="id_new_password1" placeholder="New Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password2" id="id_new_password2" placeholder="New Password" required>
</div>
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Change Password</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
表格已经在它的实例中有错误。出于某种原因,人们使用消息框架来显示错误,这实际上比简单地使用模板中的表单更困难。该视图将表单实例传递给您的模板,以便您可以使用它来呈现表单并呈现它的错误。在您的模板中试试这个:
呈现非字段错误:
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
要呈现字段错误,请使用 form.field_name.errors
:
{% for error in form.new_password1.errors %}
{{ error }}
{% endfor %}
作为注释手动呈现表单(通过自己编写标签<input ....>
)无缘无故地增加了制作表单的难度。也许有人可能不喜欢使用默认呈现的表单,但是有一些方法可以在表单的 class 中或使用一些包来自定义它。请参阅 django-widget-tweaks and django-crispy-forms 两个很棒的包来自定义表单呈现。
我按照之前的回答解决了问题。我在 HTML 中添加了错误标签,这使它完美地工作并使用一些样式很好地显示了错误。
{% for error in form.old_password.errors %}
<div class="alert alert-danger with-close alert-dismissible fade show">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% for error in form.new_password1.errors %}
<div class="alert alert-danger with-close alert-dismissible fade show">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% for error in form.new_password2.errors %}
<div class="alert alert-danger with-close alert-dismissible fade show">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
<div class="login-form">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>Current Password</label>
<input class="au-input au-input--full" type="password" name="old_password" id="id_old_password" placeholder="Current Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password1" id="id_new_password1" placeholder="New Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password2" id="id_new_password2" placeholder="New Password" required>
</div>
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Change Password</button>
</form>
</div>
我正在处理一些用户忘记密码以及使用 Django 内置功能重置密码的问题,但我正在使用我的模板来呈现页面和表单。我想要一种方法来呈现错误消息或成功消息,以便用户了解在密码太短的情况下发生了什么。
urls.py
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.Dashboard, name='dashboard'),
path('login/', views.Login, name='login'),
path('logout/', views.Logout, name='logout'),
path('register/', views.Register, name='register'),
path('forgetpassword/', views.ForgetPassword, name='forgetpassword'),
path('password_change/', auth_views.PasswordChangeView.as_view(
template_name='auth/password/change-password.html'), name='password_change'),
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(
template_name='auth/password/change-password-done.html'), name='password_change_done'),
变化-password.html
{% extends 'auth/base.html' %}
{% load static %}
{% block content %}
<div class="page-wrapper" style="height: 100vh!important;">
<div class="page-content--bge5">
<div class="container">
<div class="login-wrap">
<div class="login-content">
<div class="login-logo">
<a href="#">
<img src="{% static 'images/icon/logo-new.png' %}" alt="CoolAdmin">
</a>
</div>
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{message.tags}} with-close alert-dismissible fade show">
{{message}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% endif %}
<div class="login-form">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>Current Password</label>
<input class="au-input au-input--full" type="password" name="old_password" id="id_old_password" placeholder="Current Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password1" id="id_new_password1" placeholder="New Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password2" id="id_new_password2" placeholder="New Password" required>
</div>
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Change Password</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
表格已经在它的实例中有错误。出于某种原因,人们使用消息框架来显示错误,这实际上比简单地使用模板中的表单更困难。该视图将表单实例传递给您的模板,以便您可以使用它来呈现表单并呈现它的错误。在您的模板中试试这个:
呈现非字段错误:
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
要呈现字段错误,请使用 form.field_name.errors
:
{% for error in form.new_password1.errors %}
{{ error }}
{% endfor %}
作为注释手动呈现表单(通过自己编写标签<input ....>
)无缘无故地增加了制作表单的难度。也许有人可能不喜欢使用默认呈现的表单,但是有一些方法可以在表单的 class 中或使用一些包来自定义它。请参阅 django-widget-tweaks and django-crispy-forms 两个很棒的包来自定义表单呈现。
我按照之前的回答解决了问题。我在 HTML 中添加了错误标签,这使它完美地工作并使用一些样式很好地显示了错误。
{% for error in form.old_password.errors %}
<div class="alert alert-danger with-close alert-dismissible fade show">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% for error in form.new_password1.errors %}
<div class="alert alert-danger with-close alert-dismissible fade show">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
{% for error in form.new_password2.errors %}
<div class="alert alert-danger with-close alert-dismissible fade show">
{{error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endfor %}
<div class="login-form">
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>Current Password</label>
<input class="au-input au-input--full" type="password" name="old_password" id="id_old_password" placeholder="Current Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password1" id="id_new_password1" placeholder="New Password" required>
</div>
<div class="form-group">
<label>New Password</label>
<input class="au-input au-input--full" type="password" name="new_password2" id="id_new_password2" placeholder="New Password" required>
</div>
<button class="au-btn au-btn--block au-btn--green m-b-20" type="submit">Change Password</button>
</form>
</div>