Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Django Forms
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Tzu-ping Chung
February 23, 2016
Programming
280
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
Transcript
32 slides
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')
More Decks by Tzu-ping Chung
See All by Tzu-ping Chung
Datasets: What it is, and how it was made
uranusjr
0
240
Let’s fix extras in Core Metadata 3.0
uranusjr
0
730
Python Packaging: Why Don’t You Just…?
uranusjr
1
300
這樣的開發環境沒問題嗎?
uranusjr
9
2.8k
Django After Web 2.0
uranusjr
3
2.3k
We Store Cheese in A Warehouse
uranusjr
1
510
The Python You Don’t Know
uranusjr
17
3.4k
Python and Asynchrony
uranusjr
0
450
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
220
[DroidKnights 2026] Workflow AX 적용기 — 자동 정책 정리부터 E2E까지
dayeon
0
110
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
370
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
690
はじめてのPLATEAU
nokonoko1203
1
660
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
200
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
180
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
200
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
210
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
170
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
340
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
190
Featured
See All Featured
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.5k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
Side Projects
sachag
456
43k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
490
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
580
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
570
Google's AI Overviews - The New Search
badams
0
1.7k
We Are The Robots
honzajavorek
0
450