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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Tzu-ping Chung
February 23, 2016
Programming
260
1
Share
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
200
Let’s fix extras in Core Metadata 3.0
uranusjr
0
650
Python Packaging: Why Don’t You Just…?
uranusjr
1
270
這樣的開發環境沒問題嗎?
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
SkillsをS3 Filesに置く時のあれこれ
watany
3
1.7k
Structured Concurrency, Scoped Values and Joiners in the JDK 25 26 27
josepaumard
1
160
Agent Skills を社内で育てる仕組み作り
jackchuka
1
2.2k
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
1
660
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
0
100
リセットCSSを1行消したらアクセシビリティが向上した話
pvcresin
4
520
2026年のソフトウェア開発を考える(2026/05版) / Software Engineering Scrum Fest Niigata 2026 Edition
twada
PRO
24
14k
Firefoxにコントリビューションして得られた学び
ken7253
2
170
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
120
Migrations : C'est une question d'hygiène !
vinceamstoutz
0
730
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
470
書き換えて学ぶTemporal #fukts
pirosikick
2
380
Featured
See All Featured
Building the Perfect Custom Keyboard
takai
2
770
Designing for Performance
lara
611
70k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
120
Code Review Best Practice
trishagee
74
20k
It's Worth the Effort
3n
188
29k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Building Adaptive Systems
keathley
44
3k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
200
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
55k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
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')