I have found many similar questions to this issue. This question was one of them, but it didn't solve my problem, so I will ask my own question.
I'm making a password reset page on my website. But when I go to http://localhost:8000/users/reset-password
and enter my email and clicks on 'Reset my password', then Django throws a NoReverseMatch
error at me.
The error is:
NoReverseMatch at /users/reset-password/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
I believe there's something wrong with how I write my urlpatterns.
I've tried:
urls.py:
"""Defines URL Patterns for users."""
from django.urls import re_path
from django.contrib.auth.views import (
LoginView, PasswordResetView, PasswordResetConfirmView,
PasswordResetDoneView,
)
from . import views
urlpatterns = [
# Login Page.
re_path(r'^login/$', LoginView.as_view(template_name='users/login.html'),
name='login'),
# Logout Page.
re_path(r'^logout/$', views.logout_view, name='logout'),
# Registration Page.
re_path(r'^register/$', views.register, name='register'),
# Password reset Page.
re_path(r'^password_reset/$', PasswordResetView.as_view(
# This is the only line I added in this file.
template_name='users/password_reset_email.html'
),
name='password_reset'),
# Password reset done Page.
re_path(r'^password_reset/done/$', PasswordResetDoneView.as_view(),
name='password_reset_done'),
# Password reset confirm Page.
re_path(r'^password_reset/confirm/'
+ '(?P<uidb64>[0-9A-Za-z]+)/(?P<token>.+)/$',
PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
]
My own users/password_reset_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 'users: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 %}
Update:
I got something right. Now I get a NoReverseMatch at /users/password_reset/
Reverse for 'password_reset_confirm' with keyword arguments '{'uidb64': '', 'token': ''}' not found. 1 pattern(s) tried: ['users/password_reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$']
. I got to this error by using my own template of djangos password_reset_email.html
, where I modified the line: {% url 'password_reset_confirm' uidb64=uid token=token %}
to {% url 'users:password_reset_confirm' uidb64=uid token=token %}
. Now i'm almost certain that i'm just writing my urls or regexes wrong.
I have edited my question to show the new code.
I finally got it. Here is the answer to the problem.
Now I get a ConnectionRefusedError
, which means that I now only need to set up an SMTP Server for emails, and then it should work! The thing I was missing all the time was, that I wasn't pointing out what email template I wanted to use. I just set the email template as the template, so Django couldn't render it correctly. Here is the updated code for urls.py
where path(r'password-reset/')
is changed:
from django.urls import path, reverse_lazy
import django.contrib.auth.views as auth_views
from . import views
urlpatterns = [
# Login Page.
path(r'login/', auth_views.LoginView.as_view(
template_name='users/login.html'
),
name='login'),
# Logout Page.
path(r'logout/', views.logout_view, name='logout'),
# Registration Page.
path(r'register/', views.register, name='register'),
# Password reset page.
path(r'password-reset/', auth_views.PasswordResetView.as_view(
email_template_name='users/password_reset_email.html',
success_url=reverse_lazy('users:password_reset_done')
), name='password_reset'),
# Password reset done page.
path(r'password-reset/done/',
auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
# Password reset confirm page.
path(r'password-reset/confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm')
]
Everything else is correct. I got the answer from this answer.