Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Class-based Views: patterns and anti-patterns
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Bruno Renié
June 04, 2012
Programming
1.7k
9
Share
Class-based Views: patterns and anti-patterns
Bruno Renié
June 04, 2012
More Decks by Bruno Renié
See All by Bruno Renié
Visibility for web developers
brutasse
3
470
Decentralization & real-time with PubSubHubbub
brutasse
1
170
Deployability of Python Web Applications
brutasse
17
2.4k
Stop writing settings files
brutasse
21
2.6k
Packager son projet Django
brutasse
4
580
Staticfiles : tout ce qu'il faut savoir, rien que ce qu'il faut savoir
brutasse
4
570
Introduction to Django
brutasse
3
440
Other Decks in Programming
See All in Programming
Swift Concurrency Type System
inamiy
0
350
Running Swift without an OS
kishikawakatsumi
0
380
へんな働き方
yusukebe
6
2.9k
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
230
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
270
仕様漏れ実装漏れをなくすトレーサビリティAI基盤のご紹介
orgachem
PRO
9
5.1k
「話せることがない」を乗り越える 〜日常業務から登壇テーマをつくる思考法〜
shoheimitani
1
120
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
140
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
200
今からFlash開発できるわけないじゃん、ムリムリ! (※ムリじゃなかった!?)
arkw
0
190
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
2
260
L’IA au service des devs : Anatomie d'un assistant de Code Review
toham
0
220
Featured
See All Featured
First, design no harm
axbom
PRO
2
1.2k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.4k
Six Lessons from altMBA
skipperchong
29
4.2k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
480
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
150
So, you think you're a good person
axbom
PRO
2
2k
Context Engineering - Making Every Token Count
addyosmani
9
800
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
260
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
30 Presentation Tips
portentint
PRO
1
270
Designing Powerful Visuals for Engaging Learning
tmiket
1
330
Transcript
Bruno Renié djangocon.eu 2012 Class-based Views patterns & anti-patterns
New in Django 1.3 Better in Django 1.4
Controversy http://lukeplant.me.uk/blog/posts/djangos-cbvs-were-a-mistake/ http://www.boredomandlaziness.org/2012/05/djangos-cbvs-are-not-mistake-but.html
The view contract in Django
# Your code def view(request): return HttpResponse('yay') urlpatterns = patterns('',
r'^$', view) # Django callback, args, kwargs = resolve(request.path_info) response = callback(request, *args, **kwargs)
A view is a callable that takes a request and
returns a response
Deprecation function-based generic views are being deprecated function-based views aren't
going anywhere
Other class-based stuff
class Middleware(object): def process_request(self, request): ... Middleware
class PageAdmin(admin.ModelAdmin): def get_changelist(self, request, **kwargs): ... Admin
single instance shared across requests
from django.views import generic class Users(generic.ListView): ... users = Users.as_view()
CBV api
as_view()?
class View(object): @classonlymethod def as_view(cls, **init): def view(request, *args, **kwargs):
self = cls(**init) return self.dispatch(request, *args, **kwargs) return view
class View(object): @classonlymethod def as_view(cls, **init): def view(request, *args, **kwargs):
self = cls(**init) return self.dispatch(request, *args, **kwargs) return view Thread-safety self.request self.args self.kwargs
Declarative vs. imperative
ccbv.co.uk
(biased) usage tips
Keep urls.py for URL definition Decorate in views.py
from django.utils.decorators import method_decorator def cbv_decorator(decorator): def _decorator(cls): cls.dispatch =
method_decorator(decorator)(cls.dispatch) return cls return _decorator @cbv_decorator(login_required) class MyView(generic.ListView): pass Decorating
Decorating class MyView(generic.ListView): pass my_view = MyView.as_view() my_view = login_required(my_view)
Decorating from django.views.decorators.cache import cache_page class CacheMixin(object): cache_timeout = 60
def dispatch(self, *args, **kwargs): return cache_page(self.cache_timeout)( super(CacheMixin, self).dispatch )(*args, **kwargs) class CachedView(CacheMixin, ListView): cache_timeout = 100 @cyberdelia — https://gist.github.com/1231560
MRO Extend, don't override unless you're 100% sure of what
you're doing
class MyView(generic.FormView): def get_initial(self): initial = super(MyViews, self).get_initial() initial.update({ 'foo':
'bar', 'other': 'thing', }) return initial
Case studies
Form processing class MyView(generic.FormView): def get_form_kwargs(self): kw = super(MyView, self).get_form_kwargs()
kw['user'] = self.request.user return kw def form_valid(self, form): form.save() return super(MyView, self).form_valid(form)
Form processing class MyForm(forms.Form): def __init__(self, *args, **kwargs): self.user =
kwargs.pop('user') super(MyForm, self).__init__(*args, **kwargs) def save(self): # self‐contained, user is known
Nested navigation class Level1(generic.TemplateView): template_name = 'level_1.html' def get_context_data(self, **kwargs):
ctx = super(Level1, self).get_context_data(**kwargs) ctx['stuff'] = do_some_work() return ctx class Level2(Level1): template_name = 'level2.html' def get_context_data(self, **kwargs): ctx = super(Level2, self).get_context_data(**kwargs) ctx['other_stuff'] = level_2_work() return ctx
Drop-in features class CleverPaginator(object): paginate_by = 100 def get_count(self): raise
NotImplementedError def get_paginate_by(self, queryset): count = self.get_count() if count > self.paginate_by * 1.5: return self.paginate_by return count class CountryView(CleverPaginator, ListView): def get_count(): return self.country.num_people
Registration
django-registration is great, but… I want more template variables I'm
not using contrib.auth I want to send an SMS instead of an email …
Writing a custom backend is not as simple as subclassing
the default views from le_social.registration import views class Register(views.Register): form_class = SMSRegistrationForm def send_notification(self): ... Sane, easily overridable defaults pip install django-le-social
Settings
Does it have to be that global? Is it a
switch people will want to flip at any time? Could it be… a class attribute/method instead? I know, I'll add a setting
ACCOUNT_EXPIRES_IN from le_social.registration import views class Activate(views.Activate): expires_in = 3600
* 24 * 7 # 7 days
SOCIAL_AUTH_LOGIN_REDIRECT_URL SOCIAL_AUTH_BACKEND_ERROR_URL from le_social.twitter import views class Callback(views.Callback): def success(self,
auth): ... def error(self, message): ...
CBVs are great but don't use them for everything
class Handler500(generic.TemplateView): template_name = '500.html'
None
class View(object): def dispatch(self, request, *args, **kwargs): handler = getattr(self,
request.method.lower(), self.http_method_not_allowed) return handler(request, *args, **kwargs)
Fantastic API for shipping reusable views Good API for extending
and plugging features in existing code
As a FBGV replacement: more power, less simplicity Don't limit
yourself to Django's implementation. Use the base View and your creativity
@brutasse
[email protected]