IMPORTANT UPDATE: the approach proposed in this article is bad. Read this one instead.
You can start building a user interface using Django’s awesome CRUD in a matter of seconds, for example with just this server side configuration:
url(delete/(?P<pk>\d+)/$', generic.DeleteView.as_view(model=YourModel), name='yourapp_yourmodel_delete'),
It doesn’t matter how long you’ve been using Django, this kind of trick is always charming. When you have a deadline, you know you can get Django out of the way in a single line of code, and work on the templates.
A rich user interface might need to work with many Django views. It might even need to reverse urls like
yourapp_yourmodel_delete, which take an argument in the regexp…
- another user suggested that this same reverse view could respond directly with the result of the url, which might be a good idea after all.
Working around having to reverse the regexp
A quick and easy way to do clear the way is to use the PkUrlKwarg mixin:
This will make
/update/?pk=123 to behave like
/update/123/, eliminating the need to reverse urls:
# url url(r'tab/delete/$', login_required(TabDeleteView.as_view()), name='form_designer_tab_delete'), # view class TabDeleteView(PkUrlKwarg, TabSecurity, AjaxDeleteView): model = Tab
Quite controversial isn’t it ?
Django users know very the importance of clean, usable urls. I myself know how to write many urls to directly access a resource I often need. And that never is an edit url - except side-wide configuration for example
/configure/ which is also pretty simple and usable.
At most, there can be nice delete urls like this:
- To be easily usable by Humans, your blog delete url should be:
blogDeleteUrl + 'pk=1234'.