5xum
5xum

Reputation: 5555

Adding namespace in urls causes error that should not even be there

I have a very strange problem I really don't understand.

I am using django-registragion-redux and an almost unchanged login.html template provided by the package.

My urls.py file looks like so:

urlpatterns = [
    url(r'^', include('main_site.urls', namespace='main'), name='main'),
    url(r'^accounts/', include('registration.backends.default.urls')),
    url(r'^admin/', include(admin.site.urls))
]

The template templates/registration/login.html contains two relevant lines, along with filler:

<p>{% trans "Not a member?" %} <a href="{% url 'registration_register' %}">{% trans "Register now!" %}</a>.</p>
<p>{% trans "Forgot your password?" %} <a href="{% url 'auth_password_reset' %}">{% trans "Reset it" %}</a>.</p>

Using these settings, everything works perfectly fine. I go to http://127.0.0.1:8000/accounts/login/ and the basic site is there, and the links are set correctly.

However, I want to have a cleaner code and was advised to use namespaces. OK, no problem. I change my urls to

urlpatterns = [
    url(r'^', include('main_site.urls', namespace='main'), name='main'),
    url(r'^accounts/', include('registration.backends.default.urls', namespace='accounts')),
    url(r'^admin/', include(admin.site.urls))
]

and I change my template to

<p>{% trans "Not a member?" %} <a href="{% url 'accounts:registration_register' %}">{% trans "Register now!" %}</a>.</p>
<p>{% trans "Forgot your password?" %} <a href="{% url 'accounts:auth_password_reset' %}">{% trans "Reset it" %}</a>.</p>

Now, when I open the page http://127.0.0.1:8000/accounts/login/, I get the following error message:

NoReverseMatch at /accounts/login/
Reverse for 'auth_password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/login/
Django Version: 1.8.3
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'auth_password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Now this is strange for two reasons:

  1. The namespacing obviously works. The accounts:registration_register tag gets reversed nicely. If I only keep the second paragraph in the file, there is no error.
  2. The error states that the reverse for auth_password_reset_done cannot be found, but there is no mention of auth_password_reset_done in the template! The error actually occurs trying to reverse auth_password_reset!

Upvotes: 1

Views: 190

Answers (1)

Daniel Hepper
Daniel Hepper

Reputation: 29967

The problem is that django-registration-redux reverses some URLs in registration/auth_urls. As no namespace is used, reversing the URL fails.

There are two possible workarounds:

  • instead of including the urls.py (and implicitely auth_urls.py) from django-registration-redux, copy them into your project and fix the calls to reverse_lazy

  • fork django-registration-redux, fix the issue and submit a pull request

Upvotes: 2

Related Questions