Django reset_password_confirm TemplateSyntaxError problem - django

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

Related

Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name

I want to use the default django.contrib.auth.views for resetting passwords with email confirmation. All this code is on urls.py :
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
app_name = 'houses'
urlpatterns = [
# Root and details page
url(r'^$', views.index, name='index'),
url(r'^(?P<house_id>[0-9]+)/$', views.view_house, name='index'),
# Register / login / logout
url(r'^register/$', views.UserFormView.as_view(), name='register'),
url(r'^login/$', views.login_user, name='login_user'),
url(r'^logout/$', views.logout_user, name='logout_user'),
# User profiles and edit profiles
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
# ---- ERRORS ARE HERE ---- change / reset passwords
url(r'^change_password/$', views.change_password, name='change_password'),
url(r'^password_reset/$', auth_views.password_reset,
{'post_reset_redirect': 'houses:password_reset_done',}, name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),
url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
]
No matter what I try i keep getting:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Is it because my app name is houses? I've been trying things for hours with no luck.
This error is because Django expects to find the url password_reset_complete in the project urls and not in the app urls, so for you to keep that url in the app you need to rewrite the template password_template_email.html and passes it in the url password_reset pass the param email_template_name:
path('reset_password/',
auth_views.PasswordResetView.as_view(
template_name="users/registration/password_reset.html",
email_template_name = 'users/registration/password_reset_email.html',
success_url=reverse_lazy('users:password_reset_done')),
name="reset_password"),
and in the template password_reset_email, pass
{% autoescape off %}
To initiate the password reset process for your Account {{ user.email }},
click the link below:
{{ protocol }}://{{ domain }}{% url 'youapp:password_reset_confirm' uidb64=uid token=token %}
If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.
{% endautoescape %}
I hope I helped!
If you are using reverse url in template
{% url 'houses:password_reset_confirm' uidb64=<uidb64> token=<token> %}
and if you using reverse url in python code then use
reverse('houses:password_reset_confirm', args=(<uidb64>,<token>,))
Here, <uidb64> means uidb64 value
<token> means token value
I had the same error before, to fix this I just wrote the URLs of the reset password option in the main URLs file (project_name/urls.py) and I've add in the templates folder a folder named "registration" that has inside a file named password_reset_email.html ( it contains the email that will be sent to the user, something like:
{% autoescape off %}
To initiate the password reset process for your Account {{ user.email }},
click the link below:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.
{% endautoescape %}
). you can check my code on github if you want to see it clearly https://github.com/Ninou01/customer-relationship-management-system/blob/master/crm1/urls.py

Is there a way to change the password_reset_confirm link sent by django?

I'm using the django inbuilt authentication. When a password reset email is sent to the user it contains a link. I want to be able to simply change that link to something else. Because I have a front-end angularJS application. So, I want the link in the email to be something my AngularApp can intercept.
This is the template that sends the email https://github.com/django/django/blob/master/django/contrib/admin/templates/registration/password_reset_email.html#L6
I simply want to change {% url 'password_reset_confirm' uidb64=uid token=token %} to http://localhost:8001/passwordResetConfirm/uid/token
Just redefine the url with the name 'password_reset_confirm' after the inclusion of django.contrib.auth.urls. If there is several urls with the same name then the URL dispatcher uses the last occurrence:
from django.contrib.auth import views as auth_views
urlpatterns = patterns('',
...
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^passwordResetConfirm/(?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'),
)

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

Django NoReverseMatch

I have the following setup:
/landing_pages
views.py
urls.py
In urls.py I have the following which works when I try to access /competition:
from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"),
)
My views.py has something like this:
def page(request, page_name):
return HttpResponse('ok')
Then in a template I'm trying to do this:
{% load url from future %}
<a href="{% url 'landing_pages.views.page' page_name='competition'%}">
Competition
</a>
Which I apparently can't do:
Caught NoReverseMatch while rendering: Reverse for 'landing_pages.views.page' with arguments '()' and keyword arguments '{'page_name': u'competition'}' not found.
What am I doing wrong?
You ask in your comment to DrTyrsa why you can't use args or kwargs. Just think about it for a moment. The {% url %} tag outputs - as the name implies - an actual URL which the user can click on. But you've provided no space in the URL pattern for the arguments. Where would they go? What would the URL look like? How would it work?
If you want to allow the user to specify arguments to your view, you have to provide a URL pattern with a space for those arguments to go.
{% url [project_name].landing_pages.views.page page_name='competition' %}
Or better
{% url competition_landing 'competition' %}