【问题标题】:PasswordResetConfirmView..as_view() in Django redirect to Django administration siteDjango 中的 PasswordResetConfirmView..as_view() 重定向到 Django 管理站点
【发布时间】: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 %}
templates/registration/logged_out.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_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 管理密码重置确认页面。我附上了截图。这里。提前致谢

标签: python django


【解决方案1】:

我能想到的问题是 Django 不知道在哪里搜索重置密码 HTML 文件,这就是它显示默认密码的原因。 要覆盖默认值,您需要将template_name 传递给as_view,以便它知道要渲染什么。下面给出一个例子。

path('reset_password/',
     auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html.html'),
     name='reset_password'),

【讨论】:

  • 嗨@abdullah_adeel,感谢您的回复。我试过这个,但我开发的定制网站没有显示。它重定向到 Django 管理密码重置页面。谢谢
  • 好的,你可以试试别的,试试把文件夹的名字从templates/registration/&lt;you_file_name&gt;改成templates/EmailRegs/&lt;your_file_names&gt;。在 urls.py 中更改它以及实际的文件夹名称。
  • 太好了。如果问题已解决,请将您的问题标记为已解决,以便其他人也可以从答案中受益。
猜你喜欢
  • 1970-01-01
  • 2013-12-31
  • 2011-02-24
  • 2011-11-28
  • 2021-06-07
  • 2017-04-20
  • 2019-08-18
  • 2018-09-20
  • 2010-11-04
相关资源
最近更新 更多