0

I've been learning Django for sometime now. I came across url reverse, understood most of it but couldn't understand namespaces:

1. How is it useful ?
2. How to use it ?

It is poorly documented and I've not found any decent article on it. :(


Also could someone explain how to reverse included urls??

8
  • 1
    It has to do with the situation where you need to deploy serveral instances of the same app and the use of named urls. Let's say you've made a voting app and you need to have multiple instances of this due to one needing one type of model and the other some other model. These two apps will then share the named urls and Django will then not guarantee which instance of the app will get which url. Commented Jul 19, 2013 at 12:38
  • @limelights but for each request there can be only one instance of an app right? Commented Jul 19, 2013 at 12:56
  • Yes, but you could have different domains routing to different instances Commented Jul 19, 2013 at 13:03
  • @limelights I guess I won't understand it until I need it, what's its role in reversing included urls ?? can included urls be reversed without namespace ? Commented Jul 19, 2013 at 13:13
  • Making sure the right URL goes to the right instance of your apps. And yes, they can. Commented Jul 19, 2013 at 13:18

1 Answer 1

1

i found a good solution here Anyone knows good Django URL namespaces tutorial?

Disclaimer : This is written by David Eyk on above mentioned link

Agreed, the docs for this are rather confusing. Here's my reading of it (NB: all code is untested!):

In apps.help.urls:

urlpatterns = patterns(
    '',
    url(r'^$', 'apps.help.views.index', name='index'),
    )

In your main urls.py:

urlpatterns = patterns(
    '',
    url(r'^help/', include('apps.help.urls', namespace='help', app_name='help')),
    url(r'^ineedhelp/', include('apps.help.urls', namespace='otherhelp', app_name='help')),
    )

In your template:

{% url help:index %}

should produce the url /help/.

{% url otherhelp:index %}

should produce the url /ineedhelp/.

{% with current_app as 'otherhelp' %}
    {% url help:index %}
{% endwith %}

should likewise produce the url /ineedhelp/.

Similarly, reverse('help:index') should produce /help/.

reverse('otherhelp:index') should produce /ineedhelp/.

reverse('help:index', current_app='otherhelp') should likewise produce /ineedhelp/.

Like I said, this is based on my reading of the docs and my existing familiarity with how things tend to work in Django-land. I haven't taken the time to test this.

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

1 Comment

Thanx this was helpful specially regarding included urls

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.