简体   繁体   中英

Passing kwargs in Url Patterns

I am using the PasswordResetView of django.contrib.auth.views. I am trying to direct my path('reset-password/'...) to use my 'rest_password_email.html' template after the user has submitted an email address for password reset. I have added this as a kwargs, however django is not recognizing it and continues to direct the application to default 'password_reset_email.html'. Any suggestions on how this can be achieved? thanks.

BTW: I am using namespace 'accounts'. Hence the reason for doing the above is to introduce the amended url in my template to account for the namespace.

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 %}

You can set your email_template_name in PasswordResetView,

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'),

or you can directly pass it in .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'),

The connection error issue is resolved and the reset password is working now. I was missing an Email backend in settings.py. Included: EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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