Allow namespaced reusable app urls as ROOT_URLCONF

86 views
Skip to first unread message

Vlastimil Zíma

unread,
Feb 15, 2018, 8:40:07 AM2/15/18
to Django developers (Contributions to Django itself)
Hi,

I've tried to understand how to write an urls module for reusable app and I think I have it correctly:

# myapp.urls
app_name
= 'myapp'
urlpatterns
= [
    url
(r'^index/$', MyIndexView.as_view(), name='index'),
    url
(r'^form/$', MyFormView.as_view(), name='form'),
   
...
]


In code I use the reverses in form 'myapp:index' and 'myapp:form'. The problem is, that these urls may be the only one used by the Django project, hence I expected ROOT_URLCONF = 'myapp.urls' should work as well. But such shortcut ends up in "'myapp' is not a registered namespace" error. To avoid that, one needs to add another urls module only to include the `myapp.urls`, which seems unnecessary.

Is there a reason not to use namespace from root urlconf? Can we change that, i.e. make root resolver to load the app_name of the root urls? Did I missed something else?

Regards,
Vlastimil

Florian Apolloner

unread,
Feb 15, 2018, 10:57:54 AM2/15/18
to Django developers (Contributions to Django itself)
Hi,


On Thursday, February 15, 2018 at 2:40:07 PM UTC+1, Vlastimil Zíma wrote:
Is there a reason not to use namespace from root urlconf? Can we change that, i.e. make root resolver to load the app_name of the root urls? Did I missed something else?

I don't think there is any reason to not allow it. That said the usefulness seems kinda questionable to me (ie if you enable the admin you already have another app for the root urlconf). In that sense I'd say: patches welcome and there is nothing obvious speaking against merging them as long as they don't introduce performance issues.

Cheers,
Florian

Tim Graham

unread,
Feb 15, 2018, 12:03:44 PM2/15/18
to Django developers (Contributions to Django itself)
The issue was raised some months ago in https://code.djangoproject.com/ticket/28413 and came to a similar conclusion. The resolution was to document the restriction.

Vlastimil Zíma

unread,
Feb 16, 2018, 9:08:33 AM2/16/18
to Django developers (Contributions to Django itself)
Based on the discussions I've seen, it looks like the problem is not looked at from the correct angle. The request is not to namespace the ROOT_URLCONF, but to respect namespace if a ROOT_URLCONF has one. The use case is to prevent creation of one extra file, just to include the only application urls. It would come handy in testing and deploying a single-application projects. It may also promote a correct usage of namespaces in application urls, since it would remove the need to construct extra urlconf module for the application tests.

Imagine having the application as defined in my first email. If I want to test it, I can't just use `@override_settings(ROOT_URLCONF='myapp.urls')`, I have to create single line urls.py for the tests, just to have the include which would not ignore the namespace. In my opinion, this commonly leads to just removing the namespace from the application URLs, which makes the problem apparently go away.

The #28413 also resulted in somewhat questionable output with no change at all. Apparently that wasn't good call, since this problem keeps recurring.

I suggest to do one of these things:
 * Take the ROOT_URLCONF namespace into account if it have any.
 * Document that ROOT_URLCONF can't have namespace AND provide a warning through Django system check framework.

Personally, I'd go for the first option, which I think has negligible impact on the performance and may promote a correct usage of the URL namespaces in third party applications.

Regards
Vlastimil

Dne čtvrtek 15. února 2018 18:03:44 UTC+1 Tim Graham napsal(a):
Dne čtvrtek 15. února 2018 18:03:44 UTC+1 Tim Graham napsal(a):
Dne čtvrtek 15. února 2018 18:03:44 UTC+1 Tim Graham napsal(a):

Tim Graham

unread,
Feb 16, 2018, 9:26:00 AM2/16/18
to Django developers (Contributions to Django itself)
Feel free to reopen the ticket if you want to provide a patch.

Florian Apolloner

unread,
Feb 16, 2018, 11:54:13 AM2/16/18
to Django developers (Contributions to Django itself)


On Friday, February 16, 2018 at 3:08:33 PM UTC+1, Vlastimil Zíma wrote:
but to respect namespace if a ROOT_URLCONF has one.

I understood it like that; but as I said I didn't see many valid usecases. Your point about testing though is a good one!
Reply all
Reply to author
Forward
0 new messages