2

In my last question I asked how to get urls working for parameter before included urls.py and it worked. Django {% url %} when urls with parameters like: url(r'^foo/<parameter>/$', include(some.urls))

Now I want to use the same included urls.py with namespaces.

urls.py

urlpatterns = patterns('',
    url(r'^/foo/(?P<parameter_1>\d+)/', include('bar.urls', namespace='foo', app_name='foo')),
    )

bar.urls.py

urlpatterns = patterns('',
    url(r'^/bar/$', 'bar.views.index', name='bar'),
    url(r'^/bar/(?P<parameter_2>\d+)/$', 'bar.views.detail', name='bar_detail'),
    )

To get the url in template I use:

1. {% url foo:bar parameter_1=1 %} or {% url for:bar 1 %}
2. {% url foo:bar_detail parameter_1=1 parameter_2=1 %} or {% url foo:bar_detail 1 1 %}

I expect to get the url: 1. /foo/1/bar/ and 2. /foo/1/bar/1 but it does not work.

Interesting: if I call:

1. {% url foo:bar %}
2. {% url foo:bar_detail parameter_2=1 %} or {% url foo:bar_detail 1 %}

I get the urls: 1. /foo/(?P<parameter_1>%5Cd+)/bar/ and 2. /foo/(?P<parameter_1>%5d+)/bar/1

My question: Did i have a fault in my code or is the code not useful for, what i want to do.

1 Answer 1

2

I searched again and found the fault. There's a bug in the /django/core/urlresolvers.py.

I found the ticket at https://code.djangoproject.com/ticket/11559#no1.

To fix the problem you have to replace the /django/core/urlresolvers.py with the changed file from https://github.com/django/django/commit/02dcbe3317.

After restarting the def-server with python manage.py runserver the url-tags are resolved correct. I get /foo/1/bar/ from {% url foo:bar 1 %} instead of /foo/(?P<parameter_1>%5Cd+)/bar/.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting! I had the same problem and found out it was a bug in django's reverse method. Unfortunately, my project was using a (painfully) old version of Django (1.3). I ended up changing the url structure so parent urlconf didn't capture any parameters. But do you know a way to solve this, which doesn't imply updating to the newest django version? (Which i guess is something i will ultimately end up doing)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.