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
Django Forms
Search
Tzu-ping Chung
February 23, 2016
Programming
270
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Django Forms
Introduction to Django Forms for Django Girls × Taipei.py workshop.
Tzu-ping Chung
February 23, 2016
More Decks by Tzu-ping Chung
See All by Tzu-ping Chung
Datasets: What it is, and how it was made
uranusjr
0
210
Let’s fix extras in Core Metadata 3.0
uranusjr
0
660
Python Packaging: Why Don’t You Just…?
uranusjr
1
280
這樣的開發環境沒問題嗎?
uranusjr
9
2.7k
Django After Web 2.0
uranusjr
3
2.2k
We Store Cheese in A Warehouse
uranusjr
1
500
The Python You Don’t Know
uranusjr
17
3.3k
Python and Asynchrony
uranusjr
0
430
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
550
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
100
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
300
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
190
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
160
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
260
The NotImplementedError Problem in Ruby
koic
1
850
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
800
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
6.8k
Featured
See All Featured
Test your architecture with Archunit
thirion
1
2.3k
Done Done
chrislema
186
16k
We Are The Robots
honzajavorek
0
250
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
170
Being A Developer After 40
akosma
91
590k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
340
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.9k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.6k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
730
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Transcript
Django Forms
Django Forms • Forms? • Forms! • Model forms
Web Page
Web Page with Form submit
<form action="/path/to/send" method="POST" enctype="multipart/form-data"> <!-- form content --> <input type="submit">
</form>
None
<form action="/path/to/send" method="post" enctype="multipart/form-data"> • Where to send • URL
• Default: "" (current URL)
<form action="/path/to/send" method="post" enctype="multipart/form-data"> • How to send • Either
get or post • Get: postcard • Post: Enveloped • Default: get
<form action="/path/to/send" method="post" enctype="multipart/form-data"> • How to read • application/x-www-form-urlencoded
• Default • multipart/form-data • Form contains file
GET Submission Query string
POST Submission "GET /admin/ HTTP/1.1" 302 0 "GET /admin/login/?next=/admin/ HTTP/1.1"
200 1679 "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0 "GET /admin/ HTTP/1.1" 200 3407
<form action="/path/to/send" method="POST" enctype="multipart/form-data"> <!-- form content --> <input type="submit">
</form>
<input type="submit"> <button type="submit"> Submit </button>
<input type="submit"> <button type="submit"> Submit </button>
<form action="/path/to/send" method="POST" enctype="multipart/form-data"> <!-- form content --> <input type="submit">
</form>
Django Forms!
Models Database Django forms HTML forms
from django import forms class MessageForm(forms.Form): name = forms.CharField( max_length=100,
) title = forms.CharField( max_length=100, required=False, ) content = forms.CharField( widget=forms.Textarea, )
from .forms import MessageForm def message_board(request): form = MessageForm() return
render( request, 'message_board.html', {'form': form}, )
<form method="post"> {% csrf_token %} {{ form.as_p }} <button>ૹग़</button> </form>
<form method="post"> {% csrf_token %} {{ form.as_p }} <button>ૹग़</button> </form>
?
CSRF Token • Cross Site Request Forgery protection • “Recognise”
whether a form is legit • Protect the server from malicious changes
None
None
class Message(models.Model): name = models.CharField( max_length=100, ) title = models.CharField(
max_length=100, blank=True, ) content = models.TextField() created_at = models.DateTimeField( auto_now_add=True, )
class Message(models.Model): name = models.CharField( max_length=100, ) title = models.CharField(
max_length=100, blank=True, ) content = models.TextField() created_at = models.DateTimeField( auto_now_add=True, )
class MessageForm(forms.Form): # ... def save(self): data = self.cleaned_data message
= Message( name=data['name'], title=data['title'], content=data['content'], ) message.save() return message
class MessageForm(forms.Form): # ... def save(self): data = self.cleaned_data message
= Message( name=data['name'], title=data['title'], content=data['content'], ) message.save() return message
def message_board(request): if request.method == 'POST': form = MessageForm(request.POST) if
form.is_valid(): form.save() else: form = MessageForm() return render( request, 'message_board.html', {'form': form}, )
Further Reading • Model forms • Form factories • Custom
“cleaning”
Exercises • Implement message form • Implement message model, save
POST-ed form to it • Get messages from database • Display messages in template
Hint {% for m in messages %} <div class="message"> <p>{{
m.name }}</p> <h5>{{ m.title }}</h5> <div>{{ m.content|linebreaks }}</div> </div> {% endfor %} Message.obejcts.order_by('-created_at')