Django密码重置

3

我很新手使用Django,正在尝试为我的Django应用程序构建身份验证框架。当我尝试构建password_reset和password_reset_done应用程序时,它无法正常运行。我正在使用Django内置框架,没有进行任何自定义。

以下是我的URL:

from django.conf.urls import url
from django.contrib import admin
from . import views
from django.contrib.auth import views as auth_views



  url(r'^change-password/$', views.change_password, name='change_password'),
    url(r'^password_reset/$', auth_views.PasswordResetView.as_view(template_name="registration/password_reset.html"), name='password_reset'),
    url(r'^password_reset_done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    url(r'^password_reset_confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    url(r'^password_reset_complete/$',auth_views.PasswordResetCompleteView.as_view(), name="password_reset_complete"),

这是我收到的错误信息

NoReverseMatch at /partners/password_reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Request Method: POST
Request URL:    http://127.0.0.1:8000/partners/password_reset/
Django Version: 2.1.1
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Exception Location: C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\urls\resolvers.py in _reverse_with_prefix, line 622
Python Executable:  C:\Users\User\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.0
Python Path:    
['C:\\Users\\User\\Desktop\\protectandserve',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\django-2.1.1-py3.7.egg',
 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\pytz-2018.5-py3.7.egg']
Server time:    Thu, 4 Oct 2018 07:49:46 +0000


Error during template rendering
In template C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\contrib\admin\templates\registration\password_reset_email.html, error at line 6

Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
1   {% load i18n %}{% autoescape off %}
2   {% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
3   
4   {% trans "Please go to the following page and choose a new password:" %}
5   {% block reset_link %}
6   {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
7   {% endblock %}
8   {% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
9   
10  {% trans "Thanks for using our site!" %}
11  
12  {% blocktrans %}The {{ site_name }} team{% endblocktrans %}
13  
14  {% endautoescape %}
15  


  [1]: https://istack.dev59.com/eGmJT.webp

screen grab 1

screen grab 2


如果问题缺少代码,你认为有人如何重现这个错误? - ZF007
请发布您的 password_reset_confirm 定义。 - Vishal Raghavan
请问您需要在以上已列出的代码之外添加什么额外的代码吗?@ZF007。我没有定义password_reset_confirm,因为我认为它是内置的。 - project_kingz
我理解auth_views.PasswordResetConfirmView.as_view()是具有定义的内置函数。 - project_kingz
url.py在另一个名为"partners"的应用程序中,而不在主项目中。 - project_kingz
显示剩余4条评论
2个回答

3
  1. Copy C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\contrib\admin\templates\registration\password_reset_email.html to partners\templates\registration\
  2. Edit line 6 of the file partners\templates\registration\password_reset_email.html to

    {{ protocol }}://{{ domain }}{% url 'partners:password_reset_confirm' uidb64=uid token=token %}
    
  3. Update your urls.py to point to right template:

    url(
        r'^password_reset/$',
        auth_views.PasswordResetView.as_view(
            template_name="registration/password_reset.html",
            email_template_name="registration/password_reset_email.html",
            success_url=reverse_lazy('partners:password_reset_done'), # might be required
        ),
        name='password_reset'
    ),
    

谢谢@Vaibhav Vishal。我似乎在/partners/password_reset/处遇到了一个TemplateDoesNotExist错误。partners/registration/password_reset_email.html - project_kingz
@Vailbhav Vishal,我已经在我的问题中添加了2个屏幕截图来展示我的结构。如果您需要任何进一步的代码,请告诉我。 - project_kingz
1
谢谢你,谢谢你,谢谢你。你是传奇! - project_kingz
1
谢谢 - 这正是解决我长达一周的问题的方法! - Laurits L. L.

0
如你所提到的urls.py属于partners应用,因此反向调用应该是针对partners进行的,在您的模板中修改 {% url 'password_reset_confirm' uidb64=uid token=token %}{% url 'partners:password_reset_confirm' uidb64=uid token=token %}

partners是应用程序名称。 这样会起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接