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
260
Django 101
kennethlove
2
1k
Views can be Classy
kennethlove
7
1.7k
There's always money in the banana(-py) stand
kennethlove
1
150
Other Decks in Programming
See All in Programming
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
1.2k
Azure AI Foundryのご紹介
qt_luigi
1
210
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
為你自己學 Python
eddie
0
520
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
700
Package Traits
ikesyo
1
210
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
ゼロからの、レトロゲームエンジンの作り方
tokujiros
3
1.1k
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
960
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
How STYLIGHT went responsive
nonsquared
96
5.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Embracing the Ebb and Flow
colly
84
4.5k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
How to Think Like a Performance Engineer
csswizardry
22
1.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.