Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Classy Views

Classy Views

Talk given at DJUGL May 2012

Marc Tamlyn

May 23, 2012
Tweet

More Decks by Marc Tamlyn

Other Decks in Technology

Transcript

  1. Replacement for Django’s old generic views New in 1.3, better

    in 1.4 So much more than the old views - a standard way to approach common tasks. Details, lists, archives, editing, template rendering Classy Views - What?
  2. A few simple examples: Classy Views - What? # urls.py

    from django.conf.urls.defaults import patterns, url from django.views.generic import RedirectView urlpatterns = patterns('', url(r'^nowhere/$', RedirectView.as_view(url='/')), )
  3. Classy Views - What? # urls.py urlpatterns = patterns('', url(r'^(?P<slug>\w+)/$',

    MyModelDetail.as_view()), ) # views.py from django.views.generic import DetailView class MyModelDetail(DetailView): model = MyModel template_name = 'a_different_template.html'
  4. Classy Views - What? # views.py from django.views.generic import UpdateView

    class ChangeChild(UpdateView): model = MyModel slug_url_kwarg = 'child_slug' context_object_name = 'update_instance' def get_queryset(self): return self.model.filter(parent__slug=self.kwargs['slug']) def get_context_data(self, **kwargs): context = super(ChangeMyModel, self).get_context_data(**kwargs) context['mine'] = self.model.objects.for_user(self.request.user) return context def get_success_url(self): if self.object.owned_by(request.user): return self.object.get_absolute_url() return '/'
  5. The ultimate tool for DRY view code No more extra_context=...,

    template_name=..., every_other_kwarg=... Easy customisation (so fantastic for libraries) Classy Views - Why?
  6. Django sees it as the way forwards - the old

    generic views will not be in Django 1.5 Classy Views - Why?
  7. Unit testing is now easy Classy Views - Why? #

    tests.py from django.test import TestCase class MyTests(TestCase): def test_view(self): obj = MyModel(slug='test-object') view = MyModelDetail(object=obj) templates = view.get_template_names() self.assertEqual(templates, [ 'custom/test-object_detail.html', 'myapp/mymodel_detail.html', ])
  8. Why not? Performance - pretty minor hit https://github.com/FZambia/django-class-based- vs-function-based Classy

    Views - Why? Class Based View GET: 12.6605019569 Class Based View POST: 16.5869309902 Function Based View GET: 12.0667870045 Function Based View POST: 16.1339828968
  9. Some common patterns: login_required and other functional decorators do it

    in urls.py decorate dispatch (with a Mixin obviously) class decorators Classy Views - How?
  10. PrepareMixin HTTP method independent permission/sanity checking Classy Views - How?

    class PrepareMixin(object): def dispatch(self, request, *args, **kwargs) self.request = request self.args = args self.kwargs = kwargs resp = self.prepare() if resp: return resp return super(PrepareMixin, self).dispatch(...)