【问题标题】:Passing kwargs in Url Patterns在 URL 模式中传递 kwargs
【发布时间】:2020-06-13 14:46:53
【问题描述】:

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

顺便说一句:我正在使用命名空间“帐户”。因此,这样做的原因是在我的模板中引入修改后的 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 %}

【问题讨论】:

    标签: python django


    【解决方案1】:

    您可以在 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'),
    

    【讨论】:

    • 谢谢。我尝试使用您的第二个选项,但它给出了连接错误:请求方法:POST 请求 URL:127.0.0.1:8000/account/reset-password Django 版本:3.0.3 异常类型:ConnectionRefusedError
    • 确保您的模板表单方法是 POST
    • 我是。
    【解决方案2】:

    连接错误问题已解决,重置密码现在可以使用。我在 settings.py 中缺少一个电子邮件后端。包括:EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2023-03-29
      • 2017-05-14
      相关资源
      最近更新 更多