Class Based Views§

Class Based Views§

Using Class Based Views§

from django.views.generic import View

class ContactList(View):

    def get(self):

        return HttpResponse("You have no contacts")

Using a Template§

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).\
        context['first_names'] = ['Nathan', 'Richard']

        return context

Configuring URLs§

urlpatterns = patterns('',
        (r'^index/$', ContactList.as_view()),

Idiomatic Class Based Views§

Template Views§


Forms in Views§


Editing Views§

CreateView, UpdateView

HTTP Methods§

Writing Composable Views§


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).\

        context['event'] = self.get_event()

        return context