【问题标题】:Keep getting this error of Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name不断收到“未找到“password_reset_confirm”的反向错误。 'password_reset_confirm' 不是有效的视图函数或模式名称
【发布时间】:2021-05-30 01:21:51
【问题描述】:

嘿伙计们,我在我的网络应用程序上执行此功能以重置密码时不断收到此错误 不知道为什么会这样 这是我的urls.py 代码:

from django.urls import path
from .views import *
from django.contrib.auth import views as auth_views

完整的追溯:

NoReverseMatch at /accounts/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/accounts/password_reset/
Django Version: 3.1.6
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\Dominique\Desktop\STUFF\dev\car_sales\env\lib\site-packages\django\urls\resolvers.py, line 685, in _reverse_with_prefix
Python Executable:  C:\Users\Dominique\Desktop\STUFF\dev\car_sales\env\Scripts\python.exe
Python Version: 3.8.5
Python Path:    
['C:\\Users\\Dominique\\Desktop\\STUFF\\dev\\car_sales\\src',
 'c:\\users\\dominique\\appdata\\local\\programs\\python\\python38-32\\python38.zip',
 'c:\\users\\dominique\\appdata\\local\\programs\\python\\python38-32\\DLLs',
 'c:\\users\\dominique\\appdata\\local\\programs\\python\\python38-32\\lib',
 'c:\\users\\dominique\\appdata\\local\\programs\\python\\python38-32',
 'C:\\Users\\Dominique\\Desktop\\STUFF\\dev\\car_sales\\env',
 'C:\\Users\\Dominique\\Desktop\\STUFF\\dev\\car_sales\\env\\lib\\site-packages']
Server time:    Sun, 28 Feb 2021 06:42:15 +0000

app_name = 'accounts'

urlpatterns = [
    path('create-user/', registerview, name='register'),
    path('login/', loginview, name='login'),
    path('logout/', logoutview, name='logout'),

    path('password_reset/', auth_views.PasswordResetView.as_view(template_name='accounts/reset_password.html'), name="password_reset"),
    path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(template_name='accounts/reset_password_sent.html'), name="password_reset_done"),
    path('password_reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='accounts/reset_password_form.html'), name="password_reset_confirm"),
    path('password_reset_complete/', auth_views.PasswordResetCompleteView.as_view(template_name='accounts/reset_password_sucess.html'), name="password_reset_complete"),
]

这是我用来覆盖默认模板的模板之一reset_password.html

<h2>Introduza o email para mudar a sua password</h2>

<form method="POST">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Enviar email de atualizacao de password">
</form>

另一个错误:

{% load i18n %}{% autoescape off %}
2   {% blocktranslate %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktranslate %}
3   
4   {% translate "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   {% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
9   
10  {% translate "Thanks for using our site!" %}
11  
12  {% blocktranslate %}The {{ site_name }} team{% endblocktranslate %}
13  
14  {% endautoescape %}

我知道为什么会发生这种情况,因为我在 url 中发送了 uidb 和令牌......

我使用 GMAIL 发送电子邮件的 SMTP 配置:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'my_email'
EMAIL_HOST_PASSWORD = 'my_email_password'

【问题讨论】:

  • 请发布完整回溯。
  • @WillemVanOnsem 完成!

标签: django url-pattern reset-password


【解决方案1】:

罪魁祸首是app_name = 'accounts'。这意味着在PasswordResetView 中定义的success_url 指向一个名为password_reset_done 的视图,因此不再可以找到它的视图。 PasswordResetConfirmView 也会发生同样的情况。

您可以使用包含命名空间前缀的视图名称覆盖它:

from django.urls import path, reverse
from .views import *
from django.contrib.auth import views as auth_views

#    namespace &downarrow; (view names need to be prefixed with 'accounts:')
app_name = 'accounts'

urlpatterns = [
    path('create-user/', registerview, name='register'),
    path('login/', loginview, name='login'),
    path('logout/', logoutview, name='logout'),

    path(
        'password_reset/',
        auth_views.PasswordResetView.as_view(
            template_name='accounts/reset_password.html'
            success_url=reverse_lazy('accounts:password_reset_done')
        ),
        name='password_reset'
    ),
    path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(template_name='accounts/reset_password_sent.html'), name='password_reset_done'),
    path(
        'password_reset/<uidb64>/<token>/',
        auth_views.PasswordResetConfirmView.as_view(
            template_name='accounts/reset_password_form.html'
            success_url=reverse_lazy('accounts:password_reset_complete')
        ),
        name='password_reset_confirm'
    ),
    path('password_reset_complete/', auth_views.PasswordResetCompleteView.as_view(template_name='accounts/reset_password_sucess.html'), name='password_reset_complete'),
]

在模板中,你也应该在它前面加上命名空间,所以:

{% url '<b>account:</b>password_reset_confirm' uidb64=uid token=token %}

如果这是内置模板,您可以创建自己的模板,例如复制粘贴 original template [GitHub],然后保存新模板并指定该模板的路径:

from django.urls import path, reverse
from .views import *
from django.contrib.auth import views as auth_views

#    namespace ↓ (view names need to be prefixed with 'accounts:')
app_name = 'accounts'

urlpatterns = [
    path('create-user/', registerview, name='register'),
    path('login/', loginview, name='login'),
    path('logout/', logoutview, name='logout'),

    path(
        'password_reset/',
        auth_views.PasswordResetView.as_view(
            template_name='accounts/reset_password.html',
            success_url=reverse_lazy('accounts:password_reset_done'),
            email_template_name='path_to/template.html'
        ),
        name='password_reset'
    ),
    path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(template_name='accounts/reset_password_sent.html'), name='password_reset_done'),
    path(
        'password_reset/<uidb64>/<token>/',
        auth_views.PasswordResetConfirmView.as_view(
            template_name='accounts/reset_password_form.html'
            success_url=reverse_lazy('accounts:password_reset_complete')
        ),
        name='password_reset_confirm'
    ),
    path('password_reset_complete/', auth_views.PasswordResetCompleteView.as_view(template_name='accounts/reset_password_sucess.html'), name='password_reset_complete'),
]

【讨论】:

  • 好的,会尝试这样做并给你一个反馈,还有一件事,你能给我解释一下reverse_lazysuccess_url是什么
  • @filipe: reverse_lazy 确定给定视图名称的 URL,success_url 是表单成功时浏览器重定向到的 URL。
  • 请注意!只是一件事,现在有同样的错误 idk 为什么......我已经放在上面了
  • @filipe:但是你重写了两个paths,并在reverse_lazy 部分改写了accounts:
  • 我刚刚意识到我在尝试做什么......我坚持我有一个用户有一个特定的电子邮件..但我没有所以它没有发送任何东西,但是现在当我使用一个电子邮件时注册,它就像一个魅力!所以我会把你的答案作为正确的答案,因为我遇到的错误都已修复,再次感谢你的耐心和时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2019-03-17
  • 2020-01-02
  • 2018-11-19
  • 2019-04-06
  • 2019-08-13
  • 2019-11-04
相关资源
最近更新 更多