Django

Code

Ticket #682 (closed: wontfix)

Opened 3 years ago

Last modified 2 years ago

Fully decoupled URLconf's for apps.

Reported by: sa@c-area.ch Assigned to: adrian
Milestone: Component: Core framework
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

fully-decoupled-urlconfs.diff (1.2 kB) - added by sa@c-area.ch on 10/22/05 18:31:42.
fully-decoupled-urlconfs-v2.diff (1.2 kB) - added by sa@c-area.ch on 10/22/05 20:40:36.
Cleaner impl that also works with generic views.

Change History

10/22/05 18:31:42 changed by sa@c-area.ch

  • attachment fully-decoupled-urlconfs.diff added.

10/22/05 20:40:36 changed by sa@c-area.ch

  • attachment fully-decoupled-urlconfs-v2.diff added.

Cleaner impl that also works with generic views.

10/22/05 20:44:26 changed by sa@c-area.ch

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')),
)

10/23/05 05:25:54 changed 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.

11/06/05 05:41:43 changed by hugo

see also #66

02/18/06 17:08:52 changed by adrian

  • status changed from new to closed.
  • resolution set to wontfix.

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


Add/Change #682 (Fully decoupled URLconf's for apps.)




Change Properties
Action