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
Search
Kenneth Love
September 26, 2011
Programming
2
260
Class-Based Views
A short introduction to class-based views in Django 1.3. From the lightning talks at DjangoCon 2011
Kenneth Love
September 26, 2011
Tweet
Share
More Decks by Kenneth Love
See All by Kenneth Love
Those Who Care, Teach!
kennethlove
0
240
Django 101
kennethlove
2
1k
Views can be Classy
kennethlove
7
1.6k
There's always money in the banana(-py) stand
kennethlove
1
150
Other Decks in Programming
See All in Programming
Figma Dev Modeで変わる!Flutterの開発体験
watanave
0
150
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
Functional Event Sourcing using Sekiban
tomohisa
0
100
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
110
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Jakarta EE meets AI
ivargrimstad
0
620
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
240
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Featured
See All Featured
Building Adaptive Systems
keathley
38
2.3k
Visualization
eitanlees
145
15k
KATA
mclloyd
29
14k
Automating Front-end Workflow
addyosmani
1366
200k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
The Pragmatic Product Professional
lauravandoore
31
6.3k
The Language of Interfaces
destraynor
154
24k
Designing for Performance
lara
604
68k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Fireside Chat
paigeccino
34
3k
Transcript
The Dumb & Lazy Guide to Class-based Views Kenneth Love
(with apologies/thanks to Daniel Greenfeld)
Old
@login_required def awesome_view(request): if request.method == “POST”: form = AwesomeForm(request.POST)
if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’)) else: form = AwesomeForm() return render(“awesome.html”, {‘form’: form}) @login_required def awesome_view(request): form = AwesomeForm(request.POST or None) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’) return render(“awesome.html”, {‘form’: form})
New
Class MyAwesomeView(TemplateView): template_name = “awesome.html” def get(self, request): form =
AwesomeForm() return self.render_to_response({‘form’: form}) def post(self, request): form = AwesomeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’)) return self.render_to_response({‘form’: form})
Class MyAwesomeView(TemplateView): template_name = “awesome.html” def get(self, request): form =
AwesomeForm() return self.render_to_response({‘form’: form}) def post(self, request): form = AwesomeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’)) return self.render_to_response({‘form’: form})
Class MyAwesomeView(TemplateView): template_name = “awesome.html” def get(self, request): form =
AwesomeForm() return self.render_to_response({‘form’: form}) def post(self, request): form = AwesomeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’)) return self.render_to_response({‘form’: form})
Class MyAwesomeView(TemplateView): template_name = “awesome.html” def get(self, request): form =
AwesomeForm() return self.render_to_response({‘form’: form}) def post(self, request): form = AwesomeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’)) return self.render_to_response({‘form’: form})
But where’s @login_required?
Class MyAwesomeView(TemplateView): template_name = “awesome.html” @method_decorator(login_required) def dispatch(self, *args, **kwargs):
return super(MyAwesomeView, self).dispatch(*args, **kwargs) def get(self, request): form = AwesomeForm() return self.render_to_response({‘form’: form}) def post(self, request): form = AwesomeForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(‘awesome’)) return self.render_to_response({‘form’: form})
URLs
from project.views import MyAwesomeView [...] url(r’^awesome/$’, MyAwesomeView.as_view(), name=”awesome”) [...]
Thanks! But we need more docs about class-based views.