Code (for django.conf.urls):
{{{
def url_tree(regex, *urls):
return url(regex, include(patterns('', *urls)))
}}}
This function cleans up url definitions that contain multiple layers. For
example if you had a urls file containing:
{{{
urlpatterns = patterns('',
url(r'^$', MyListView.as_view()),
url(r'^(?P<pk>\d+)/$', MyItemView.as_view()),
url(r'^(?P<pk>\d+)/delete/$', MyDeleteView.as_view()),
url(r'^(?P<pk>\d+)/post/$', MyPostView.as_view()),
)
}}}
And DRY it into:
{{{
urlpatterns = patterns('',
url(r'^$', MyListView.as_view()),
url_tree(r'^(?P<pk>\d+)/',
url('^$', MyItemView.as_view()),
url(r'^delete/$', MyDeleteView.as_view()),
url(r'^post/$', MyPostView.as_view()),
),
)
}}}
This function gets very useful with tens of possible functions and long
urls. So. Does this make sense as an addition to django? Is the
functionality already there an I've missed it? Let me know.
--
Ticket URL: <https://code.djangoproject.com/ticket/24074>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_better_patch: => 0
* resolution: => wontfix
* needs_tests: => 0
* needs_docs: => 0
Comment:
You can use `include()` for this, see the last example in
https://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-
urlconfs.
--
Ticket URL: <https://code.djangoproject.com/ticket/24074#comment:1>
Comment (by doctormo):
It's a bit of a lisp there with all those brackets, but I guess it's
better that DY. Thanks for the quick resolution.
--
Ticket URL: <https://code.djangoproject.com/ticket/24074#comment:2>