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
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
290
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
170
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
100
Jakarta EE meets AI
ivargrimstad
0
610
みんなでプロポーザルを書いてみた
yuriko1211
0
260
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
210
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
イベント駆動で成長して委員会
happymana
1
320
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Featured
See All Featured
Designing for humans not robots
tammielis
250
25k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
How to Ace a Technical Interview
jacobian
276
23k
Code Review Best Practice
trishagee
64
17k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Building Your Own Lightsaber
phodgson
103
6.1k
RailsConf 2023
tenderlove
29
900
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Automating Front-end Workflow
addyosmani
1366
200k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
No one is an island. Learnings from fostering a developers community.
thoeni
19
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.