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
1
240
Django Forms
Introduction to Django Forms for Django Girls × Taipei.py workshop.
Tzu-ping Chung
February 23, 2016
Tweet
Share
More Decks by Tzu-ping Chung
See All by Tzu-ping Chung
Datasets: What it is, and how it was made
uranusjr
0
120
Let’s fix extras in Core Metadata 3.0
uranusjr
0
480
Python Packaging: Why Don’t You Just…?
uranusjr
1
210
這樣的開發環境沒問題嗎?
uranusjr
9
2.5k
Django After Web 2.0
uranusjr
3
2k
We Store Cheese in A Warehouse
uranusjr
1
440
The Python You Don’t Know
uranusjr
17
3k
Python and Asynchrony
uranusjr
0
350
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
선언형 UI에서의 상태관리
l2hyunwoo
0
180
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
500
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
830
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
280
モバイルアプリにおける自動テストの導入戦略
ostk0069
0
110
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
790
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
470
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
340
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
940
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
98
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Designing Experiences People Love
moore
138
23k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Visualization
eitanlees
146
15k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
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')