Code

Opened 9 years ago

Closed 8 years ago

Last modified 8 years ago

#682 closed enhancement (wontfix)

Fully decoupled URLconf's for apps.

Reported by: sa@… Owned by: adrian
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: UI/UX:

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@… 9 years ago.
fully-decoupled-urlconfs-v2.diff (1.2 KB) - added by sa@… 9 years ago.
Cleaner impl that also works with generic views.

Download all attachments as: .zip

Change History (6)

Changed 9 years ago by sa@…

Changed 9 years ago by sa@…

Cleaner impl that also works with generic views.

comment:1 Changed 9 years ago by sa@…

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 Changed 9 years ago by hugo

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 Changed 9 years ago by hugo

see also #66

comment:4 Changed 8 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.