以Url模式传递kwargs



我正在使用django.contrib.auth.views的PasswordResetView。在用户提交了重置密码的电子邮件地址后,我正试图将我的路径('reset-password/'…(定向为使用我的'rest_password_email.html'模板。我已经将其添加为kwargs,但是django没有识别它,并继续将应用程序引导到默认的"password_reset_email.html"。关于如何实现这一点,有什么建议吗?谢谢

BTW:我正在使用命名空间"accounts"。因此,进行上述操作的原因是在我的模板中引入修改后的url来说明命名空间。

url.py

from django.urls import path 
from . import views
from django.contrib.auth.urls import urlpatterns
from django.contrib.auth.views import (
LoginView, LogoutView, 
PasswordResetView, PasswordResetDoneView,
PasswordResetConfirmView,
PasswordResetCompleteView
)
from django.urls import reverse_lazy
app_name = 'accounts'
urlpatterns = [
path('', views.home, name = 'home'),
path('column/', views.column),
path('login/', LoginView.as_view(template_name='accounts/login.html'), name = 'login'),
path('logout/', LogoutView.as_view(template_name='accounts/logout.html'), name = 'logout'),
path('register/', views.register, name = 'register'),
path('profile/', views.view_profile, name = 'view_profile'),
path('profile/edit/', views.edit_profile, name = 'edit_profile'),
path('change_password/', views.change_password, name = 'change_password'),
path('reset-password/', PasswordResetView.as_view(template_name='accounts/reset_password.html'), kwargs={'email_template_name':'accounts/reset_password_email.html','post_reset_redirect': reverse_lazy('accounts:password_reset_done')}, name = 'password_reset'),
path('reset-password/done', PasswordResetDoneView.as_view(), name = 'password_reset_done'),
path('reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', PasswordResetConfirmView.as_view(), name = 'password_reset_confirm'),
path('reset-password/complete/$', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

reset_password_email.html

{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}

您可以在PasswordResetView、中设置email_template_name

url(r'^reset-password/$',
PasswordResetView.as_view(template_name='accounts/reset_password.html'),
{
'email_template_name': 'accounts/reset_password_email.html',
'success_url' : reverse_lazy('accounts:reset_password_done')
},
name='reset_password'),

或者您可以直接将其传入.as_view((

url(r'^reset-password/$',
PasswordResetView.as_view(template_name='accounts/reset_password.html',
email_template_name = 'accounts/reset_password_email.html',
success_url = reverse_lazy('accounts:reset_password_done'))  ,
name='reset_password'),

连接错误问题已解决,重置密码现在可以工作。我在settings.py.Included:Email_backend='django.core.mail.backends.console.EmailBackend'中缺少电子邮件后端

最新更新