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 + Flask
Search
Tzu-ping Chung
September 08, 2015
Programming
1
210
Django + Flask
Configure Flask for Django 1.8.
Tzu-ping Chung
September 08, 2015
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
500
Python Packaging: Why Don’t You Just…?
uranusjr
1
220
這樣的開發環境沒問題嗎?
uranusjr
9
2.6k
Django After Web 2.0
uranusjr
3
2.1k
We Store Cheese in A Warehouse
uranusjr
1
440
The Python You Don’t Know
uranusjr
17
3.1k
Python and Asynchrony
uranusjr
0
360
Graphics on Raspberry Pi with Qt 5
uranusjr
0
96k
Other Decks in Programming
See All in Programming
Domain-Driven Transformation
hschwentner
2
1.9k
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
300
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
240
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
660
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
170
2,500万ユーザーを支えるSREチームの6年間のスクラムのカイゼン
honmarkhunt
6
5.1k
Honoとフロントエンドの 型安全性について
yodaka
4
250
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
29
11k
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
4
1.3k
CNCF Project の作者が考えている OSS の運営
utam0k
5
690
Conform を推す - Advocating for Conform
mizoguchicoji
3
680
DROBEの生成AI活用事例 with AWS
ippey
0
130
Featured
See All Featured
Building Your Own Lightsaber
phodgson
104
6.2k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
How to train your dragon (web standard)
notwaldorf
90
5.8k
YesSQL, Process and Tooling at Scale
rocio
171
14k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
29
4.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Building Adaptive Systems
keathley
40
2.4k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Speed Design
sergeychernyshev
25
780
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
99
18k
Transcript
Django + Jinja2
Django Templates • The Django Template Language • How Django
loads templates
Django Templates • The Django Template Language • How Django
loads templates DTL
<ul> {% for user in users.all %}
<li> <a href="{{ user.url }}"> {{ user.username|capitalize }} </a> </li> {% endfor %} </ul>
<ul> {% for user in users.all %}
<li> <a href="{{ user.url }}"> {{ user.username|capitalize }} </a> </li> {% endfor %} </ul> (Template) Tag
<ul> {% for user in users.all %}
<li> <a href="{{ user.url }}"> {{ user.username|capitalize }} </a> </li> {% endfor %} </ul> Variable
<ul> {% for user in users.all %}
<li> <a href="{{ user.url }}"> {{ user.username|capitalize }} </a> </li> {% endfor %} </ul> Variable + Filter
The DTL • Simple syntax with few rules • Lightweight
extensions • Self-contained
But… • Awkward DSL • No scoped functions • Slow
with frequent rendering
Django 1.8 • django.template • django.template.backends
TEMPLATE_CONTEXT_PROCESSORS TEMPLATE_DEBUG TEMPLATE_STRING_IF_INVALID TEMPLATE_DIRS TEMPLATE_LOADERS The
Language The System
TEMPLATE_CONTEXT_PROCESSORS TEMPLATE_DEBUG TEMPLATE_STRING_IF_INVALID TEMPLATE_DIRS TEMPLATE_LOADERS The
Language The System
TEMPLATES = [ {
'BACKEND': '...', 'DIRS': [], 'OPTIONS': { 'context_processors': [...], 'debug': False, 'string_if_invalid': '', }, }, ] Language-specific Setup Template-loading Setup
None
Jinja2 • Armin Ronacher (aka mitsuhiko) • Inspired by the
DTL • Standalone library • More programmer-friendly
<ul> {% for user in users.all() %}
<li> <a href="{{ user.url }}"> {{ user.username.toupper() }} </a> </li> {% endfor %} </ul> Function calling
<ul> {% for user in users.get_friends(me) %}
<li> <a href="{{ user.url }}"> {{ user.username.toupper() }} </a> </li> {% endfor %} </ul>
Settings for Jinja2 { 'BACKEND': (
'django.template.backends.' 'jinja2.Jinja2'), 'DIRS': [], 'APP_DIRS': True, },
demoapp ├── jinja2 │ └── a_jinja2_template.html ├── models.py ├── templates
│ └── a_django_template.html ├── urls.py └── views.py
demoapp ├── jinja2 │ └── a_jinja2_template.html ├── models.py ├── templates
│ └── a_django_template.html ├── urls.py └── views.py Configurable (not recommended)
Jinja2 for Django • Auto-escape on • Custom template loader
• Debug enhancements • Auto-reload • Undefined raises exceptions
But • Jinja2 is not built (just) for Web •
Web-related functionalities • Django internals
{% url 'pages:page' name=page_name %} {% static 'base/css/site.min.css' %}
{% trans 'This is my website!' %} {% csrf_token %}
https://github.com/MoritzS/jinja2-django-tags
{ 'BACKEND': ('django.template.backends.'
'jinja2.Jinja2'), 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'extensions': [ 'jdj_tags.extensions.DjangoStatic', 'jdj_tags.extensions.DjangoI18n', ] }, },
Jinja2 vs DTL • Functions vs template tags • Methods
vs filters • Extensions are loaded by project
The Flask Way • {% url_for(endpoint, **kwargs) %} • Endpoint
'static' • {% get_flashed_messages(...) %} • I18n API (already pretty similar) • {{ csrf_token() }}
To boldly go where no one has gone before.