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
250
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
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
3
300
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
7
1.3k
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
500
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
160
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
280
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
Go の GC の不得意な部分を克服したい
taiyow
3
800
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
110
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Mobile First: as difficult as doing things right
swwweet
222
9k
Typedesign – Prime Four
hannesfritz
40
2.4k
How to Ace a Technical Interview
jacobian
276
23k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
It's Worth the Effort
3n
183
28k
A Tale of Four Properties
chriscoyier
157
23k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
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.