#24074 closed New feature (wontfix)
Addition: Url convenience function
Reported by: | Martin Owens | Owned by: | nobody |
---|---|---|---|
Component: | Core (URLs) | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
We're using a convenience function in our website urls and I want to propose it's inclusion into django upstream. I'm sounding out developers feelings on such an addition before I generate a patch and connected documentation.
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.
Change History (2)
comment:1 by , 10 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 10 years ago
It's a bit of a lisp there with all those brackets, but I guess it's better than RY. Thanks for the quick resolution.
You can use
include()
for this, see the last example in https://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs.