Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#682 closed enhancement (wontfix)

Fully decoupled URLconf's for apps.

Reported by: sa@… Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In part 3 of the tutorial you introduce a way to decouple the URLconfs.
But unless I'm missing something there's still one thing left that is tied to the project, namely the prefix passed to the patterns() function.

urlpatterns = patterns('myproject.apps.polls.views.polls',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

The attached patch eliminates the need to pass in the prefix and let's the patterns() function figure that out by itself. Pass None instead of the prefix to trigger the "figure-out-the-prefix-by-yourself" functionality.

urlpatterns = patterns(None,
    ...
)

Hope that's usefull for someone.

cheers

Attachments (2)

fully-decoupled-urlconfs.diff (1.2 KB ) - added by sa@… 19 years ago.
fully-decoupled-urlconfs-v2.diff (1.2 KB ) - added by sa@… 19 years ago.
Cleaner impl that also works with generic views.

Download all attachments as: .zip

Change History (6)

by sa@…, 19 years ago

by sa@…, 19 years ago

Cleaner impl that also works with generic views.

comment:1 by sa@…, 19 years ago

Usage of v2:

With standard views:

urlpatterns = patterns(prefix('views.polls'),
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

With generic views:

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
    (r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results')),
    (r'^(?P<poll_id>\d+)/vote/$', prefix('views.polls.vote')),
)

comment:2 by hugo, 19 years ago

Maybe "prefix" could be "module_prefix"? Because if we get something like #672 in, there will be two different prefixes in the urlconf and it would be nice to be able to distinguis then :-)

Another idea could be to start auto-detect-prefixes with a ".". That would make your samples this:

urlpatterns = patterns('.views.polls',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
    (r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results')),
    (r'^(?P<poll_id>\d+)/vote/$', '.polls.vote'),
)}}}

A starting dot would make the urlmatchers add the application module prefix to the name.

comment:3 by hugo, 19 years ago

see also #66

comment:4 by Adrian Holovaty, 19 years ago

Resolution: wontfix
Status: newclosed

A bit too magic for my tastes -- but feel free to use it yourself, of course.

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