Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoReverseMatch on password_Reset_confirm

Tags:

django

I have a problem getting password_Reset_confirm bit working.

url:

(r'^password_reset/$', 'django.contrib.auth.views.password_reset'),
(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
(r'^password_reset_confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),

password_reset_email.html, which includes this:

{% load url from future %}
Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb36=uid token=token %}

But then after submitting my email for reseting the password, I get this error message shown:

NoReverseMatch at /password_reset/ Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{'uidb36': '1', 'token': '38d-b5ec0b2a2321f522f954'}' not found.

I thought since this was using a build in view, I wouldn't have to take care on anything else?

Thanks for advice,

Update:

After using the full path, it seems to work. However it sends two emails out: and each has a different link. Why is that? And where do I set the {{ domain }}? Thanks

Follow the link below:
http://example.com/password_reset_confirm/1-38d-b5ec0b2a2321f522f954/

Follow the link below:
http://example.com/password_reset_confirm/2-38d-18482e1f129c84b9c2bc/

Update 2

I figured it out. Just in case someone else has this problem. You need to set your domain name as the Site for your application:

In Admin or django console:

>>> my_site = Site.objects.get(pk=1)
>>> my_site.domain = 'somedomain.com'
>>> my_site.name = 'Some Domain'
>>> my_site.save()

The other problem why you could get two emails when resetting it, is because that you can have multiple usernames associated with the same email address. Its pretty silly. This is the next thing I have to tackle down.

like image 943
Houman Avatar asked Jun 21 '12 17:06

Houman


3 Answers

To pass a url to the url template tag, you can specify a name to the url in the urls.py

url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 
views.password_reset_confirm, name='password_reset_confirm'),

and then you can use the tag with the url name

{% url 'password_reset_confirm' uidb64=uid token=token %}
like image 116
Shih-Wen Su Avatar answered Dec 03 '22 02:12

Shih-Wen Su


When using the url template tag, you need to specify the view and not the url itself. Since you are using 'django.contrib.auth.views.password_reset_confirm' in your URLConf you should use it like this:

{% url 'django.contrib.auth.views.password_reset_confirm' ... %}

More on the url template tag on Django's Built-in template tags and filters documentation.

like image 30
César Avatar answered Dec 03 '22 00:12

César


Be sure to have this in your urls.py:

urlpatterns = [
    url('^', include('django.contrib.auth.urls'))
]

See https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.views.password_reset Section: Authentication views

like image 29
Davy Avatar answered Dec 03 '22 01:12

Davy