Django : No Reverse Match on password reset [duplicate] - django

This question already has answers here:
What is a NoReverseMatch error, and how do I fix it?
(6 answers)
NoReverseMatch exception while resetting password in django using django's default views
(1 answer)
Closed 5 years ago.
I am working on password reset functionality in Django using custom templates.
But when I entered mail:id and clicking on reset password I'm facing "No Reverse match error".Please someone help me,I understand this is an old problem, I have followed other answers and tried solving but could not come up with solution.Please find the code below and help me solve this problem.
urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from django.contrib.auth.views import password_reset,password_reset_done,password_reset_confirm,password_reset_complete
from surveysite.views import home,about,login,loggedin,loggedout
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^about/$',about, name='about'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^accounts/login/$', login, name='login'),
url(r'^accounts/loggedin/$',loggedin, name='loggedin'),
url(r'^accounts/logout/$', loggedout, name='logout'),
url(r'^accounts/password_reset/$', password_reset,
{'post_reset_redirect' : '/accounts/password_reset/mailed/'},
name="password_reset"),
url(r'^accounts/password_reset/mailed/$',password_reset_done),
url(r'^accounts/password_reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',password_reset_confirm,
{'post_reset_redirect' : '/accounts/password_reset/complete/'},name="password_reset_confirm"),
url(r'^accounts/password_reset/complete/$',password_reset_complete),
url(r'^admin/', admin.site.urls),
]
Password_reset_email
{% autoescape off %}
You're receiving this email because you requested a password reset for your user account at {{ site_name }}.
Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb36=uid token=token %}
{% endblock %}
Your username, in case you've forgotten: {{ user.get_username }}
Thanks for using our site!
The {{ site_name }} team
{% endautoescape %}
Error Stack Trace
NoReverseMatch at /accounts/password_reset/
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'MQ', u'token': u'4jz-530973903a3b600e2a97'}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL: http://localhost:8000/accounts/password_reset/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'MQ', u'token': u'4jz-530973903a3b600e2a97'}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python27\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable: C:\Python27\python.exe
Python Version: 2.7.13
Python Path:
['G:\\Manoj\\Software Engineering-Yui Man lee\\Django_Project\\surveysite',
'C:\\Python27',
'C:\\Python27\\Tools\\Scripts',
'G:\\Manoj\\Software Engineering-Yui Man lee\\Django_Project\\surveysite',
'C:\\WINDOWS\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\PIL']
In template G:\Manoj\Software Engineering-Yui Man lee\Django_Project\surveysite\templates\registration\password_reset_email.html, error at line 6
I have tried solutions from below links but could not find the solution.
NoReverseMatch on password_Reset_confirm
https://github.com/bread-and-pepper/django-userena/issues/380
django password reset NoReverseMatch error
I'm following tutorial from http://code.techandstartup.com/django/reset-password/ ==> customized Templates.

Instead of referencing the view, you need to just reference the name of your url pattern that you are trying to match
{% url 'password_reset_confirm' uidb36=uid token=token %}

Related

django url tag generating NoReverseMatch

When using the django url tag with a dynamic parameter in a Django template, the following snippet:
<ul>
{% for m in markets %}
<li>{{ m.mic }}</li>
{% endfor %}
</ul>
is always generating a NoReverseMatch exception:
Error during template rendering
In template D:\X\django\project0\core\templates\markets\index.html, error at line 29
Reverse for 'market_detail' with keyword arguments '{'market_mic': ''}' not found. 1 pattern(s) tried: ['core/market/(?P<market_mic>[^/]+)/$']
It is configured like this in core/urls.py:
from django.urls import path
from . import views
app_name = 'core'
urlpatterns = [
# ex: /core/market/
path('market/', views.market_index, name='market_index'),
# ex: /core/market/XPAR/
path('market/<str:market_mic>/', views.market_detail, name='market_detail'),
# ex: /core/market/XPAR/all
path('market/<str:market_mic>/all', views.market_all_udls, name='market_all_udls'),
When trying to get the reverse with the shell (python manage.py shell), it works:
In [5]: reverse('core:market_detail', kwargs={'market_mic': 'XPAR'})
Out[5]: '/core/market/XPAR/'
It is likely Django is unable to resolve the market variable in the url tag while elsewhere it is rendering it as expected.

Django no reverse for django.contrib.auth.views.password_reset_confirm

I'm trying to use Django's built in authentication views for password reset, however I can't figure out why the application is bugging out for the built in authentication view password_reset_confirm. Any idea how I can fix this, or at least debug it? Been stuck on this issue for a while now.
Template error
NoReverseMatch at /accounts/password/reset/
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': u'xxxxxxxxxxxxxxxxxxxxxxx', u'token': u'xxxxxxxxxxxxxxxxx'}' not found.
Error during template rendering
In template /home/user/Envs/local/lib/python2.7/site-packages/django/contrib/admin/templates/registration/password_reset_email.html, error at line 6
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': u'xxxxxxxxxxxxxxxxxxxxxxx', u'token': u'xxxxxxxxxxxxxxxx'}' not found.
----> {{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %} <---- Template errors here
urls.py
from django.contrib.auth.views import login, password_reset, password_reset_confirm, password_reset_done, password_reset_complete
urlpatterns = patterns('userProfile.views',
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
password_reset_confirm,
name='password_reset_confirm'),
...)
Attempted Solutions
Per Joseph's suggestion, modifying the admin template fixes the problem. BUT, why can't I reverse the built in auth view???
{{ protocol }}://{{ domain }}{% url 'userProfile:password_reset_confirm' uidb64=uid token=token %}
I believe since you've given the URL mapping a name, you can use just that name in the reversing:
{% url 'password_reset_confirm' uid token %}
Assuming uid and token are in the context.
You might be able to do it the way you want to like this:
urlpatterns = patterns(
'django.contrib.auth.views',
(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', 'password_reset_confirm'),
)
Once you define a name though, I believe that's what you have to use for reversing it. Since your URL is not a tuple of string,string but of string,function,string, you can't reverse on the name of the view function, but on the name of the URL pattern (that last string).
I may be mistaken here though.
[Second update]
The first argument in patterns is a prefix. You could try doing this instead:
from django.contrib.auth.views import login, password_reset, password_reset_confirm, password_reset_done, password_reset_complete
urlpatterns = patterns('',
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$','django.contrib.auth.views.password_reset_confirm'),
url(r'...other_app_urls_here','other_view'),
...)
Or if you really want to keep the prefix for your app, just add the patterns seperately:
from django.contrib.auth.views import login, password_reset, password_reset_confirm, password_reset_done, password_reset_complete
urlpatterns = patterns('',
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$','django.contrib.auth.views.password_reset_confirm'),
)
urlpatterns += patterns('userProfile.views',
...userProfile_urls_here...
)
Either of those two solutions should allow you to reverse the whole builtin view.
Doc links:
https://docs.djangoproject.com/en/1.7/ref/urls/#django.conf.urls.patterns
https://docs.djangoproject.com/en/1.7/topics/http/urls/#urlpatterns-view-prefix
https://docs.djangoproject.com/en/1.7/topics/http/urls/#multiple-view-prefixes

NoReverseMatch in render_to_response with django-social-auth

I would like to make just a page that has link to login to twitter/facebook/google with django-social-auth.
but I get a error NoReverseMatch: Reverse for '' with arguments '(u'twitter',)' and keyword arguments '{}' not found.
def index(request):
ctx = {}
return render_to_response('index_before_login.html', {}, RequestContext(request))
index_before_login.html is following
<li>Enter using Twitter</li>
urls.py is following
urlpatterns = patterns('',
url(r'^$', 'lebabcartoon.views.index'),
#url(r'^socialauth_', 'lebabcartoon.views.index'),
url('', include('social_auth.urls')),
my environment is
Django ver1.5
Python Version: 2.7.3
django-social-auth: 0.7.5
anyideas?
Wrap url name in quotes
{% url 'socialauth_begin' 'twitter' %}
To save someone using the new python-social-auth and django > 1.4
Use this :
{% url 'social:begin' 'twitter' %}
I had a similar problem, try adding the following to the url.py file in your project.
url(r'auth/', include('social_auth.urls'))
And also make sure your url parameters are wrapped in quotes like so.
{% url "socialauth_begin" "twitter" %}

NoReverseMatch Error

I keep getting this error for the django login system. Here is part of my urls.py:
(r'^contractManagement/login', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
The exact error I am getting:
Exception Type: NoReverseMatch
Exception Value: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found.
I can't understand why i am getting this error. If you need anything else let me know.
You don't show where you are trying to reverse this URL, but it looks like you have double-quoted it. If you're using the url tag, note that you don't need quotes around the url name:
{% url django.contrib.auth.views.login %}
not
{% url 'django.contrib.auth.views.login' %}
You see that ''the.unknown.view'' is reported including too many qoutes.
It is because the quoted syntax will be valid in Django 1.5 and higher. For Django 1.3 or 1.4, you should activate the future behavior by this line in the template:
{% load url from future %}
which is valid also for Django 1.5.
Example for Django 1.5+
{% url "path.to.some.view" %}
Classic syntax for Django <= 1.4.x (without "future" command) is:
{% url path.to.some.view %}
I would give your url a name (in order to do that, you need to use the url method) Also you should add a trailing slash to all your urls, cause the django CommonMiddleware is going to be doing a 302 redirect on all your urls if you don't:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'^contractManagement/login/', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='contract_login'),
)
Then you can use reverse in your code, or url in your templates, and if you ever decide to change the actual url (ie: changedCotractManagement/login/), as long as the name is the same, your code will still be good.
in code:
from django.core.urlresolvers import reverse
reverse('contract_login')
in template:
{% url contract_login %}
Edit: per MrOodles

Django reset_password_confirm TemplateSyntaxError problem

when I use django.contrib.auth.views.password_reset_confirm without arguments at all it works and I can render the template without any problem, when adding uidb36 and token arguments it fails.
Caught NoReverseMatch while rendering: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{'uidb36': '111', 'token': '1111111111111'}' not found.
Most likely it is an issue with your urls.py. You need to setup the right pattern to grab the uidb36 and token values passed as URL parameters. If not, it will throw a similar error to what you see above.
Something like:
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', {'template_name' : 'registration/password_reset.html', 'post_reset_redirect': '/logout/' })
registration/password_reset.html - is my custom template
logout - is my custom logout action
I had this issue in Django 1.3, and wasted a lot of time because the error can mask a number of underlying issues.
I needed to add this to the top of the reset email template:
{% load url from future %}
Also, the example in the Django docs didn't match the sample url:
{{ protocol}}://{{ domain }}{% url 'auth_password_reset_confirm' uidb36=uid token=token %}
So I had to change the auth_password_reset_confirm above to password_reset_confirm.
If you're using Django 1.6+ and run into something like this it could be that you need to update uidb36 to uidb64 in both your template and your urls.
Example url:
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm
and reset link in template:
{{ protocol}}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
For Django 1.8+ users, just copy this URL to your main urls.py file, so that it recognizes the URL name
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
'django.contrib.auth.views.password_reset_confirm',
name='password_reset_confirm'),
And add this mentioned by: #Lunulata to your password_reset_email.html file:
{{ protocol}}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
Try adding following to your urls.py
(r'^reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'django.contrib.auth.views.password_reset_confirm'),
I found this to work, copied from the default url
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
Just add this line to your urls.py:
url('^', include('django.contrib.auth.urls')),
This enables the django reset_password workflow.
Then override your login.html to include the line:
<div class="password-reset-link">
href="{{ password_reset_url }}">{% trans 'Forgotten your password or username?' %}</a></div>
Now you should be able to use the builtin Django PasswordResetView included with Django as long as your email settings are set up.
if you are using app_name in every urls.py
suppose you have an app and in that app in urls.py you have mentioned app_name="accounts"
in order to retrieve the page you need to mention two things
template_name and success_url inside the PasswordResetView(template_name="accounts/password_reset.html" , success_url= reverse_lazy('accounts:password_reset_sent'))
dont forget to import reverse_lazy from django.urls inside urls.py
so your final code of accounts/urls.py should look like
My app name is landing instead of accounts
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
from django.urls import reverse_lazy
app_name='landing'
urlpatterns = [
path('',views.home,name="home"),
path('terms/',views.terms,name="terms"),
path('login/',views.loginUser,name="login"),
path('signup/',views.signupUser,name="signup"),
path('about/',views.about,name="about"),
path('logout/',views.logoutUser,name="logout"),
path('password_reset/',
auth_views.PasswordResetView.as_view(template_name='landing/password_reset.html',success_url=reverse_lazy('landing:password_reset_done')),
name="password_reset"),
path('password_reset_sent/',
auth_views.PasswordResetDoneView.as_view(template_name='landing/password_reset_sent.html'),
name="password_reset_done"),
path('reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='landing/password_reset_form.html',success_url=reverse_lazy('landing:password_reset_complete')),
name="password_reset_confirm"),
path('password_reset_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='landing/password_reset_done.html'),
name="password_reset_complete"),
]
you have to use app_name: before the name of url you have mentioned it is very important