【发布时间】:2021-02-13 18:22:20
【问题描述】:
我正在使用 Django 3 开发一个通过电子邮件确认密码重置的网站。 单击电子邮件中的链接后,它将进入 Django 管理密码重置确认页面,而不是带我进入我开发的网页。我正在通过示例书关注 Django 3。这些代码来自本书的第 4 章。
请帮忙。提前致谢。
Here is the project GitHub link.
这是应用程序的 urls.py(命名为“帐户”)
urlpatterns = [
#path('login/', views.user_Login, name='login'),
# Log-in and Logout
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('',dashboard,name='dashboard'),
# change password urls
path('password_change/',auth_views.PasswordChangeView.as_view(),name='password_change'),
path('password_change/done/',auth_views.PasswordChangeDoneView.as_view(),name='password_change_done'),
# reset password urls
path('password_reset/',auth_views.PasswordResetView.as_view(),name='password_reset'),
path('password_reset/done/',auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(),name='password_reset_confirm'),
path('reset/done/',auth_views.PasswordResetCompleteView.as_view(template_engine='accounts/password_change_form.html'),name='password_reset_complete'),
]
settings.py
INSTALLED_APPS = [
'account.apps.AccountConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',]
LOGIN_REDIRECT_URL = 'dashboard'
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
templates/registration/login.html
{% extends "base.html" %}
{% block title %}Log-in{% endblock %}
{% block content%}
<h1>Log-in</h1>
{% if form.errors %}
<p>
Your username and passwoed didn't match.
Please try again.
</p>
{% else %}
<p>Please, use the following form to log-in:</p>
{% endif %}
<div class="login-form">
<form action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<p><a href="{% url "password_reset" %}">Forgotten your password?</a> </p>
<input type="hidden" name="next" value="{{ next }}">
<p><input type="submit" value="Log-in"></p>
</form>
</div>
{% endblock %}
{% extends "base.html" %}
{% block title %}Logged out{% endblock %}
{% block content %}
<h1>Logged out</h1>
<p>
You have successfully logged out.
You can <a href="{% url "login" %}">log-in-again</a>
</p>
{% endblock %}
templates/registration/password_change_done.html
{% extends "base.html" %}
{% block title %}Logged out{% endblock %}
{% block content %}
<h1>Logged out</h1>
<p>
You have successfully logged out.
You can <a href="{% url "login" %}">log-in-again</a>
</p>
{% endblock %}
templates/registration/password_change_form.html
{% extends "base.html" %}
{% block title %}Change your password{% endblock %}
{% block content %}
<h1>Change your password</h1>
<p>Use the form below to change your password.</p>
<form method="post">
{{ form.as_p }}
<p><input type="submit" value="Change"></p>
{% csrf_token %}
</form>
{% endblock %}
templates/registration/password_reset_confirm.html
{% extends "base.html" %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<h1>Reset your password.</h1>
{% if validlink %}
<p>Please enter your new password twice:</p>
<form method="post" action="password_change_form.html">
{{ form.as_p }}
{% csrf_token %}
<p><input type="submit" value="Change my password"></p>
</form>
{% else %}
<p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
{% endif %}
{% endblock %}
templates/registration/password_reset_complete.html
{% extends "base.html" %}
{% block title %}Password reset{% endblock %}
{% block content %}
<h1>Password set</h1>
<p>Your password has been set. You can <a href="{% url "login" %}">log in now</a> </p>
{% endblock %}
templates/registration/password_reset_done.html
{% extends "base.html" %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<h1>Reset your password</h1>
<p>We've emailed you instruction for setting your password.</p>
<p>If you don't receieve an email, please make sure you've enter the address you reginstered with.</p>
{% endblock %}
templates/registration/password_reset_email.html
Someone asked for password resent for email {{ email }}. Follow the link below:
{{ protocol }}://{{ domain }}{% url "password_reset_confirm" uidb64=uid token=token %}
Your username, in case you've forgotten: {{ user.get_username }}
templates/registration/password_reset_form.html
{% extends "base.html" %}
{% block title %}Reset your password{% endblock %}
{% block content %}
<h1>Forgotten your password ?</h1>
<p>Enter your e-mail address to obtain a new password.</p>
<form method="post">
{{ form.as_p }}
<p><input type="submit" value="Sent e-mail"></p>
{% csrf_token %}
</form>
{% endblock %}
电子邮件截图。 enter image description here
此链接打开此页面 enter image description here
【问题讨论】:
-
请提供最小的、可重复的示例。 How to create a Minimal, Reproducible Example
-
您好,bhucho,感谢您的回复。我的问题是电子邮件中的链接(设置新密码)。这不会带我到 templates/registration/password_change_form.html。而是将我带到 Django 管理密码重置确认页面。我附上了截图。这里。提前致谢