Skip to content

Instantly share code, notes, and snippets.

@jleeothon
Last active August 29, 2015 14:12
Show Gist options
  • Save jleeothon/9929b86d08aee5066b82 to your computer and use it in GitHub Desktop.
Save jleeothon/9929b86d08aee5066b82 to your computer and use it in GitHub Desktop.
CRUD URL Design with Python
# settings.py
APPEND_SLASH = True
# projectname/urls.py
from django.conf.urls import include
from django.conf.urls import patterns
from django.conf.urls import url
import people.customers.urls
import stock.products.urls
urlpatterns = patterns(
'',
url(r'^customers/', include(people.customers.urls)),
url(r'^products/', include(stock.products.urls)),
)
# people/customers/urls.py
from django.conf.urls import include
from django.conf.urls import patterns
from django.conf.urls import url
from people.customers import views
urlpatterns = patterns(
'',
url(r'^$', views.ListView.as_view(), name='customer-list'),
url(r'^new/$', views.CreateView.as_view(), name='customer-create'),
url(r'^(?P<pk>\d+)/', include(patterns(
url(r'^$', views.DetailView.as_view(), name='customer-detail'),
url(r'^edit/$', views.UpdateView.as_view(), name='customer-update'),
url(r'^delete/$', views.Delete.as_view(), name='customer-delete'),
))),
)
# P.S. Works great on its own, works better with https://github.com/jleeothon/urlmodel
@jleeothon
Copy link
Author

Whereas a traditional way to do it could be:

urlpatterns = patterns(
    '',
    url(r'^customers/$', ListView.as_view(), name='customer-list'),
    url(r'^customers/new/$', CreateView.as_view(), name='customer-create'),
    url(r'^customers/(?P<pk>\d+)/$', DetailView.as_view(), name='customer-detail'),
    url(r'^customers/(?P<pk>\d+)/edit/$', UpdateView.as_view(), name='customer-update'),
    url(r'^customers/(?P<pk>\d+)/delete/$', DeleteView.as_view(), name='customer-delete'),
)

The "DRYer" way is not extremely DRY either. Unless someone will make an installable module for that. It may be added to urlmodel, but I'm too busy right now. And that's arguable a good or bad idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment