Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Django + Flask
Tzu-ping Chung
September 08, 2015
Programming
1
170
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
Let’s fix extras in Core Metadata 3.0
uranusjr
0
23
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
TechFeed Conference 2022 - Kotlin Experimental
jmatsu
0
740
UI State Modeling 어떤게 좋을까?
laco2951
1
220
microCMS × Shopifyで、ECサイトがリニューアル後急成長した話
microcms
0
470
Nix for Scala folks
kubukoz
0
120
About Type Syntax Proposal
quramy
1
1.1k
Explore Java 17 and beyond
josepaumard
3
650
インフラエンジニアの多様性と評価、またはキャリアへのつなげ方 / Careers as infrastructure engineers
katsuhisa91
0
490
Let's make a contract: the art of designing a Java API
mariofusco
0
160
dbtとBigQueryで始めるData Vault入門
kazk1018
0
180
脱オブジェクト指向講座(5分LT資料)
kishida
8
11k
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
1k
iOSアプリの技術選択2022
tattn
6
2.4k
Featured
See All Featured
Done Done
chrislema
174
14k
Building Applications with DynamoDB
mza
83
4.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
350
21k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
15k
Learning to Love Humans: Emotional Interface Design
aarron
261
37k
GitHub's CSS Performance
jonrohan
1020
410k
Facilitating Awesome Meetings
lara
29
3.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
119
28k
How to Ace a Technical Interview
jacobian
265
21k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
100
5.9k
The Invisible Customer
myddelton
110
11k
How to name files
jennybc
39
58k
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.