.. slideconf:: :theme: single-level
- New in Django 1.3 (generic views)
- Allow "composing" a View from pieces
- Intended to allow more flexible reuse
- Base View plus a set of "mixins" provide composable functionality
- Lots of power, lots of [potential] complexity
- Subclass
View
- Define a method name that matches the HTTP method you're implementing
.. testcode:: from django.views.generic import View class ContactList(View): def get(self): return HttpResponse("You have no contacts")
.. testcode:: from django.views.generic import TemplateView class ContactList(TemplateView): template_name = 'index.html' # or define get_template_names() def get_context_data(self, **kwargs): context = super(ContactList, self).\ get_context_data(**kwargs) context['first_names'] = ['Nathan', 'Richard'] return context
- Django URLConf needs a callable, not a class
View
provides asas_view
method
urlpatterns = patterns('', (r'^index/$', ContactList.as_view()), )
kwargs
passed toas_view
can override properties on the View class- Arguments captured in the URL pattern are available as
.args
and.kwargs
inside your class
- Number of mixins can be confusing
- However there are a few common idioms
- Many times you don't wind up defining the HTTP methods directly, just the things you need
TemplateView
get_context_data()
template_name
,get_template_names()
response_class
render_to_response()
ProcessFormView
form_class
get_success_url()
form_valid(form)
form_invalid(form)
CreateView, UpdateView
- Includes Form processing
model
get_object()
The
http_method_names
property defines a list of supported methodsIn Django 1.5 this is:
http_method_names = ['get', 'post', 'put', 'delete', 'head', 'options', 'trace']
If you want to support something like HTTP
PATCH
, you need to add it to that list in your View subclassViews will look for a class method named for the HTTP method:
get()
is called forGET
, etc.
- Think about the extension points you need
- Call
super()
in your methods: this allows others to mix your View with others
.. testcode:: class EventsPageMixin(object): """View mixin to include the Event in template context.""" def get_event(self): if not hasattr(self, 'event'): self.event = get_event() return self.event def get_context_data(self, **kwargs): context = super(EventsPageMixin, self).\ get_context_data(**kwargs) context['event'] = self.get_event() return context
.. notslides:: * No actual view logic * Subclasses ``object``, not ``View`` * Calls ``super`` on overridden methods