Generic views allow you to write much shorter code.
Compare:
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404, redirect
from myapp.models import Context
def edit(request, item_id):
object = get_object_or_404(Context, pk=item_id)
if request.method == 'POST':
form = ContextForm(request.POST, instance=object)
if form.is_valid():
form.save()
return redirect('myapp-context-index')
else:
form = ContextForm(instance=object)
return render_to_response("myapp/context/edit.html", {'object': object, 'form': form})
with:
from django.core import urlresolvers
from django.views.generic.create_update import update_object
from myapp.models import Context
def edit(request, item_id):
return update_object(request, object_id=item_id,
form_class=ContextForm,
template_name="myapp/context/edit.html",
post_save_redirect=urlresolvers.reverse("myapp-context-index"))
Like your normal views, they are just normal functions. It is possible to configure the view completely in the URLconf if you like, through I find this usage above a bit more clear.
As a BONUS, you also get:
- Login authentication checks (pass
login_required=True
)
- Success status message from
django.contrib.messages
.
- Less code to check for errors.
- A default
ModelForm
when you provide a model
parameter instead of form_class
.
The template_name
has a default of "appname/model_form.html", but that's a bit too much for me.
Here is the form class they both share:
class ContextForm(forms.ModelForm):
"""The form for a context"""
class Meta:
model = Context
exclude = ('collection',)
def save(self, commit=True):
"""Overwritten save to force collection_id to a value"""
model = super(ContextForm, self).save(commit=False)
model.collection_id = 1
if commit:
model.save()
return model