19

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.

Houman
  • 64,245
  • 87
  • 278
  • 460
  • 3
    I think you are suppose to specify the name of the view instead of the name of the url. Something like `{% url 'django.contrib.auth.views.password_reset_confirm' ... %}` – César Jun 21 '12 at 17:55
  • Yes that has worked. Weird, because I had copied that from Django documentation. Now there are two problems left, please see updated question. Thank you – Houman Jun 21 '12 at 18:33
  • Cesar, I fixed the other two problems and updated the question. If you wish to put your comment as a reply and I will tick it off as the answer. Thanks – Houman Jun 22 '12 at 13:24

5 Answers5

12

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 %}
Shih-Wen Su
  • 2,589
  • 24
  • 21
5

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.

César
  • 9,939
  • 6
  • 53
  • 74
  • Support for string view arguments to url() is deprecated and was removed in Django 1.10. So @Shih-WenSu answer should be used instead – kostr22 Apr 19 '18 at 11:31
1

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

Davy
  • 1,720
  • 1
  • 19
  • 42
0

It might be a built-in view, but you still need a URL for it. You should define one in urls.py and link it up to the password_reset_confirm view.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I have a url defined for it already, see above.. (Unless I misunderstood you) The solution from Cesar works actually. But now I am not there yet completely. Just updated the question. Thank you – Houman Jun 21 '12 at 18:34
0

Just copy this URL to your main urls.py file, so that it recognizes the URL name

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

K2A
  • 172
  • 2
  • 7