Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Django Forms
Tzu-ping Chung
February 23, 2016
Programming
1
190
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
Let’s fix extras in Core Metadata 3.0
uranusjr
0
24
Python Packaging: Why Don’t You Just…?
uranusjr
1
110
這樣的開發環境沒問題嗎?
uranusjr
9
2k
Django After Web 2.0
uranusjr
3
1.5k
We Store Cheese in A Warehouse
uranusjr
1
350
The Python You Don’t Know
uranusjr
16
2.2k
Python and Asynchrony
uranusjr
0
260
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
You Might Not Want Async
uranusjr
6
2.2k
Other Decks in Programming
See All in Programming
脱オブジェクト指向講座(5分LT資料)
kishida
8
11k
Explore Java 17 and beyond
josepaumard
3
660
Update from the Elixir team - 2022
whatyouhide
0
200
インフラエンジニアの多様性と評価、またはキャリアへのつなげ方 / Careers as infrastructure engineers
katsuhisa91
0
540
Micro Frontends with Module Federation: Beyond the Basics @codecrafts2022
manfredsteyer
PRO
0
120
実録mruby組み込み体験
coe401_
0
110
CIでAndroidUIテストの様子を録画してみた
mkeeda
0
180
LegalForceの契約データを脅かすリスクの排除と 開発速度の向上をどうやって両立したか
aibou
0
330
Licences open source : entre guerre de clochers et radicalité
pylapp
2
340
Oculus Interaction SDK 概説 / xrdnk-caunity-LT4
xrdnk
0
250
GraphQL+KMM開発でわかったこと / What we learned from GraphQL+KMM development
kubode
0
130
byte列のbit表現を得るencodingライブラリ作った
convto
1
180
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
19
1.4k
Infographics Made Easy
chrislema
233
17k
Building Flexible Design Systems
yeseniaperezcruz
310
33k
Documentation Writing (for coders)
carmenhchung
48
2.5k
Large-scale JavaScript Application Architecture
addyosmani
499
110k
We Have a Design System, Now What?
morganepeng
35
2.9k
Stop Working from a Prison Cell
hatefulcrawdad
261
17k
Imperfection Machines: The Place of Print at Facebook
scottboms
253
11k
Agile that works and the tools we love
rasmusluckow
319
19k
Building Adaptive Systems
keathley
25
1.1k
Building Your Own Lightsaber
phodgson
94
4.6k
No one is an island. Learnings from fostering a developers community.
thoeni
9
1.1k
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')