繁体   English   中英

在 Url 模式中传递 kwargs

[英]Passing kwargs in Url Patterns

我正在使用 django.contrib.auth.views 的 PasswordResetView。 在用户提交用于密码重置的电子邮件地址后,我试图将我的路径 ('reset-password/'...) 指向我的 'rest_password_email.html' 模板。 我已将此添加为 kwargs,但是 django 无法识别它并继续将应用程序定向到默认的“password_reset_email.html”。 关于如何实现这一目标的任何建议? 谢谢。

顺便说一句:我正在使用命名空间“帐户”。 因此,这样做的原因是在我的模板中引入修改后的 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 中缺少电子邮件后端。 包括:EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM