Opened 7 years ago

Closed 7 years ago

#26288 closed New feature (wontfix)

Support relative imports in django.conf.urls.include

Reported by: Chris Lamb Owned by: nobody
Component: Core (URLs) Version: 1.9
Severity: Normal Keywords: urls
Cc: Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


For example, within myproject.urls:

    urlpatterns = (
        url(r'', include('.myapp.urls')),

.. becomes equivalent to::

    urlpatterns = (
        url(r'', include('myproject.myapp.urls')),

Whilst a contrived example, this can make heavy-nested apps look far, far cleaner. For example, within, one only needs to include .foo_bar_baz_qux.urls to get to the "next"
level, rather than the significantly more unwieldy

Change History (6)

comment:2 Changed 7 years ago by Tim Graham

Triage Stage: UnreviewedSomeday/Maybe

I created a discussion on django-developers to get feedback on the idea.

comment:3 Changed 7 years ago by Tim Graham

Marten said on the mailing list, "the whole problem can already be fixed from the user's perspective by importing the module instead of using string based imports. That is possible and has been possible for a long time. In that light, I don't see the benefit of supporting relative string based imports with some confusing edge-cases."

Any problem with that approach?

comment:4 Changed 7 years ago by Chris Lamb

Any problem with that approach?

Well, firstly it's extra stuff you have to explicitly import which seems a style regression from the "oh just use these included urls" that you get with the string-based import.

Secondly, whilst it might look fine when you are doing it once, for example:

from django.conf.urls import url, include

from . import views
from .foo import urls

urlpatterns = (
    url(r'', include(urls, namespace='foo')),

    url(r'^$', views.landing, name='landing'),

.. but the import dance get a bit nasty when you have multiple ones - you have to alias each import:

from django.conf.urls import url, include

from . import views
from .foo_app import urls as foo_urls
from .bar_app import urls as bar_urls
# etc.

urlpatterns = (
    url(r'', include(foo_urls, namespace='foo')),
    url(r'', include(bar_urls, namespace='bar')),

    url(r'^$', views.landing, name='landing'),

This isn't an readabiliy improvement over using the string urls IMHO.

Now, if only we could do the following:

from . import views, foo_app, bar_app

urlpatterns = (
    url(r'', include(foo_app.urls), namespace='foo')),
    url(r'', include(bar_app.urls), namespace='bar')),

    url(r'^$', views.landing, name='landing'),


comment:5 Changed 7 years ago by Tim Graham

Perhaps you could add your view on the mailing list discussion? Thanks!

comment:6 Changed 7 years ago by Tim Graham

Resolution: wontfix
Status: newclosed

Closing due to lack of interest on the mailing list.

Note: See TracTickets for help on using tickets.
Back to Top