Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use namespace urls with django in a reusuable app

Tags:

I have a django app, a forum app, that has templates with it. In those templates, there are urls that point to parts of the app. For instance the thread_list template has links to each thread like so:

{% for thread in threads %}     <a href="{% url forum_thread thread %}">{{thread.title}}</a> {% endfor %} 

The thing is, I don't really like calling my urls "forum_thread". I prefer just "thread" and using the namespace feature of django. "forum_thread" may be used somewhere else in the project (namespace collision).So it will look like this:

{% for thread in threads %}     <a href="{% url forum:thread thread %}">{{thread.title}}</a> {% endfor %} 

but this doesn't feel like the correct way to do this. The docs are kind of unclear here.

I want this app to be reusable and easy to configure. But I also want to use the best standards. I don't want to have the to make the user specify their own namespace name, and then have them edit every single url in each template.

How should I do urls in this app?

like image 480
priestc Avatar asked Jul 01 '10 07:07

priestc


People also ask

What is namespace in Django urls?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.

How do I add a URL to a Django project?

project/urls.py is the one that django uses, so you have to import to project/urls.py. Which means, that you have to state your imports in project/urls.py. The imported urls.py file must also define a valid urlconf, named urlpatterns. I think I had to restart the server (plus forgot the include), works now!

How the include function is used in the URL file in Django?

include() A function that takes a full Python import path to another URLconf module that should be “included” in this place. Optionally, the application namespace and instance namespace where the entries will be included into can also be specified.


2 Answers

From what I can gather you should be able use {% url forum:thread thread %} as you've described. Namespaces always seem to be defined with two variables, namespace and app_name.

If you then do the following in urls.py:

url(r'^/forum/', include('forum.urls', namespace='forum', app_name='forum')), url(r'^/foo/', include('forum.urls', namespace='foo', app_name='forum')), url(r'^/bar/', include('forum.urls', namespace='bar', app_name='forum')), 

In my understanding, this defines 3 instances of the app 'forum', 'foo', 'bar', and the default (which has namespace==app_name).

When you reverse forum:thread, it uses the current context to determine which one to use- if you are in namespace 'foo' it will use that, otherwise it will fall back on the default.

If anyone is able to clarify how Django decides what the 'current' namespace/app is that would be very helpful. I currently categorise it as 'black magic'.

Some clarification on the actual difference between namespace and app_name would also be helpful- it's possible that I have this totally reversed. The current docs are highly ambiguous.

Note: I have this working for initial requests, but I'm currently unable to make this work for AJAX requests- those always use the default instance for some reason.

like image 156
SystemParadox Avatar answered Oct 13 '22 01:10

SystemParadox


This might be a simple syntax error. I was following the Django Tutorial, and I changed mysite/urls.py improperly. The original syntax:

url(r'^polls/', include('polls.urls')), 

The desired change:

url(r'^polls/', include('polls.urls', namespace="polls")), 

What I did:

url(r'^polls/', include('polls.urls'), namespace="polls"), 

Correcting the syntax resolved the issue.

like image 22
turiyag Avatar answered Oct 12 '22 23:10

turiyag