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
990
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
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
350
gopls を改造したら開発生産性が高まった
satorunooshie
8
240
CSC305 Lecture 13
javiergs
PRO
0
130
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
400
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.6k
GCCのプラグインを作る / I Made a GCC Plugin
shouth
1
150
Identifying User Idenity
moro
6
7.9k
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.7k
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.2k
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
Featured
See All Featured
Bash Introduction
62gerente
608
210k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Git: the NoSQL Database
bkeepers
PRO
425
64k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
We Have a Design System, Now What?
morganepeng
50
7.2k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Code Review Best Practice
trishagee
64
17k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Making Projects Easy
brettharned
115
5.9k
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.