Search code examples
djangonamespacesurl-pattern

Reverse for 'password_reset_done' with arguments with namespace


Under Django 1.8. I added a namespace to my app but now I have problem with registrations pages.

Url: http://127.0.0.1:8000/accounts/password_reset/

in myapp/urls.py:

  ...
  from django.contrib.auth.views import password_reset, password_reset_done
  ...

  # include registration app urls 
  url(r'^accounts/', include('registration.urls')),


  url(r'^accounts/password_reset/$', password_reset,
      {'template_name': 'registration/password_reset.html'},
      name='reset-password'),

  url(r'^accounts/password_reset_success/$', password_reset_done,
       {'template_name': 'registration/password_reset_done.html'},
       name="password_reset_done"),

Error:

NoReverseMatch at /accounts/password_reset/
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

In project/urls/myapp.py:

url(r'^', include('myapp.urls',
    namespace='myapp', app_name='myapp')),

in django.contrib.auth.views password_reset :

If I replace

if post_reset_redirect is None:
    post_reset_redirect = reverse('password_reset_done')
else:
    post_reset_redirect = resolve_url(post_reset_redirect)
if request.method == "POST":

** with **

if post_reset_redirect is None:
    post_reset_redirect = reverse('myapp:password_reset_done')
else:
    post_reset_redirect = resolve_url(post_reset_redirect)
if request.method == "POST":

It works.

So I think I have to pass the namespace to the registrations urls somewhere.


Solution

  • You can set a custom post_reset_redirect in your URL config when you include the password_reset view:

    url(r'^accounts/password_reset/$', password_reset,
      {'template_name': 'registration/password_reset.html', 'post_reset_redirect': reverse_lazy('myapp:password_reset_done')},
      name='reset-password'),
    

    However, I think you'll find that using a namespace requires changes in other parts of the password reset process as well (e.g. the email template). The easiest solution is not to use a namespace for this app.