Confused by Namespace vs app.name and app.label

I opened this now invalid ticket, it turns out I just don’t understand the django way. Having used django for 3 years, I thought I did.

When using reverse(), or {% url '....' %}, I have always passed in the apps.py → AppConfig.name. Now reading up a bit, I think one is supposed to pass in a namespace. However what is a namespace, where does it come from? The docs talk about it, but dont explain how one sets it.

When including urls, historically I have been doing this:
path('foo/', include('applib.foo.urls')),

And it can successfully be reversed using applib.foo as the namespace (which is the AppConfig.name I was passing in).

Should it be:
path('foo/', include('applib.foo.urls', namespace='foo')),
And hence use foo as the namespace?

Are namespaces independant of AppConfig.names? I always thought they were the same thing. If they area different, why does using the app.name work instead of the namespace?

The key to namespaces is that they only apply to URL patterns when used with include().

The docs have three ways include() can be called.

include(module, namespace=None)¶
include(pattern_list)
include((pattern_list, app_namespace), namespace=None)

Usually you use the first way, just as include(module), and don’t pass a namespace at all.
The application namespace is pulled from the includes urls.py — this is the example in the docs here

The key bit to recall is that the namespace kwarg is for the instance namespace — which is how you include the same set of URL patterns multiple time.

But normally you don’t need that.

The third example — include((pattern_list, app_namespace), namespace=None) is how you pass the application namespace manually, in a tuple. Again often you’d do this without the namespace, since you don’t often include the same urls twice….

It’s a little bit gnarly to say the least, but keeping in mind the three calling patterns from the include() reference (same link as at the top here) is the way to keeping it clear.

I hope that helps.

In this example:
https://docs.djangoproject.com/en/4.0/topics/http/urls/#url-namespaces-and-included-urlconfs

It is has app_name = 'polls

If polls was within a sub dir, e.g.:
myapps/polls/apps.py

Should app_name = polls or myapps.polls ?

If the answer is polls, then surely the variable should be called app_label not app_name in ones url config?

Assuming the . doesn’t break anything :stuck_out_tongue_winking_eye:, it’s up to you. the application namespace is (not more than) a string you define to namespace a set of URLs when used in another URL conf via include(). It avoids clashes, but you have to remember to use it with reverse().

Hi Carl, thank you very much for you answers in the issue I raised and the help here in the forum. It is bery much appreciated. Explaining that the namespace system has nothing to do with the app config name caused a light bulb moment for me! due to the variable name app_name I always thought it was tied to the app config.

1 Like

The linked ticket is nominally about whether it should be, but it’s not, no. (And likely won’t change now I’d imagine… :thinking:)

Hopefully it helps! I remember being deeply confused by this for a while at some point where it all changed, such that namespace required an application namespace, where previously folks had been using namespace for the application namespace, in happy oblivion. :stuck_out_tongue_winking_eye: