SlideShare a Scribd company logo
1 of 29
Download to read offline
Django Templates
@starwilly
What We Have Known
{{ <post.title> }}
{% if post.photo %}
<div class="thumbnail">
<img src="{{ post.photo }}" alt="">
</div>
{% else %}
<div class="thumbnail thumbnail-default"></div>
{% endif %}
{% for post in post_list %}
…
{% endfor %}
{{ post.created_at|date:"Y / m / d" }}
Variable
Tag <a href="{% url 'post_detail' pk=post.pk %}">
Filter
Template Inheritance
home.html post.html
Template Inheritance
home.html
post.html
base.html
home.html
post.html
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A Django Girl's Adventure</title>
<link href=“//…/assets/css/style.css” rel="stylesheet">
</head>
<body>
<div class="header">
<h1 class="site-title text-center">
<a href="/">A Django Girl’s Adventure</a>
</h1>
</div>
<div class=“container">
{% block content %} {% endblock content %}
</div>
</body>
</html>
Extends a template
<!-- home.html -->
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
<div class="post-wrapper">
<div class="post">
…
</div>
</div>
{% endfor %}
{% endblock content %}
Extends a template
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
<div class="post-wrapper">
<div class="post">
…
</div>
</div>
{% endfor %}
{% endblock content %}
{% extends %} must be the first template tag
Fill the Block
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
<div class="post-wrapper">
<div class="post">
…
</div>
</div>
{% endfor %}
{% endblock content %}
base.html
Fill content to the block you defined
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A Django Girl's Adventure</title>
<link href=“//…/assets/css/style.css” rel="stylesheet">
</head>
<body>
<div class=“header">
<h1 class="site-title text-center">
<a href="/">A Django Girl’s Adventure</a>
</h1>
<ul class="nav navbar-nav">
<li><a href="#">About Me</a></li>
<li><a href="#">Favorite Trips</a></li>
<li><a href="#">Albums</a></li>
...
<li><a href="#">Contact Me</a></li>
</ul>
</div>
<div class=“container">
{% block content %} {% endblock content %}
</div>
</body>
</html>
Include
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A Django Girl's Adventure</title>
<link href=“//…/assets/css/style.css” rel="stylesheet">
</head>
<body>
<div class=“header">
<h1 class="site-title text-center">
<a href="/">A Django Girl’s Adventure</a>
</h1>
<ul class="nav navbar-nav">
<li><a href="#">About Me</a></li>
<li><a href="#">Favorite Trips</a></li>
<li><a href="#">Albums</a></li>
...
<li><a href="#">Contact Me</a></li>
</ul>
</div>
<div class=“container">
{% block content %} {% endblock content %}
</div>
</body>
</html>
Extract as Another Template
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A Django Girl's Adventure</title>
<link href=“//…/assets/css/style.css” rel="stylesheet">
</head>
<body>
{% include ‘_header.html’ %}
<div class=“container">
{% block content %} {% endblock content %}
</div>
</body>
</html>
Include Template
Example: base.html
<!DOCTYPE html>
{% load static %}
<html>
<head>
<link rel="stylesheet" href="main.css" />
{% block extra_css %}{% endblock extra_css %}
<title>
{% block title_tag %}
{% block title %} Welcome {% endblock title %} | A Django Girl's Adventure
{% endblock title_tag %}
</title>
</head>
<body>
{% include "_header.html" %}
<div class="container">
{% block content %} {% endblock content %}
</div>
<script src="{% static 'js/vendors/jquery.js' %}"></script>
<script src="{% static 'js/vendors/bootstrap.js' %}"></script>
{% block extra_js %}{% endblock extra_js %}
</body>
</html>
<!-- home.html -->
{% block title %}Home{% endblock title %}
<!-- post.html -->
{% block title %}{{ post.title }}{% endblock title %}
<title>
{% block title_tag %}
{% block title %} Welcome {% endblock title %} | A Django Girl's Adventure
{% endblock title_tag %}
</title>
<!-- post.html -->
{% block title_tag %}{{ post.title }}{% endblock title_tag %}
Home | A Django Girl's Adventure
| A Django Girl's Adventure
block.super
| Welcome | A Django Girl's Adventure
<!-- post.html -->
{% block title %}{{ post.title }} | {{ block.super }}{% endblock title %}
<title>
{% block title_tag %}
{% block title %} Welcome {% endblock title %} | A Django Girl's Adventure
{% endblock title_tag %}
</title>
Use {{ block.super }} to include parent’s content
Common Conventions
• Prefer underscore over dash in template name, block name
• Included name of the block in endblock tag

• Template called by other template are prefixed with `_`
{% block content %} {% endblock content %}
{% include ‘_header.html’ %}
{% block title_tag %}
2-Tier Template Architecture
templates/
base.html
home.html # extends base.html
trips/
trips_list.html # extends base.html
trips_detail.html # extends base.html
Best for websites with a consistent overall layout
from app to app
3-Tier Template Architecture
templates/
base.html
trips/
base_trips.html # extends base.html
trips_list.html # extends base_trips.html
trips_detail.html # extends base_trips.html
album/
base_album.html # extends base.html
album_list.html # extends base_album.html
album_detail.html # extends base_album.html
Best for websites where each sections requires a
distinctive layout
Flat is Better Than Nested
Zen of Python
Comment
Comment in Django Template
{% comment %}
<div class="sidebar">
sidebar content is not ready
</div>
{% endcomment %}
Multi-line Comment
{# single line comment #}
Single Line Comment
<!-- HTML Comment -->
Filters
Filters
{{ post.title | upper }}
{{ post.title | lower }}
{{ post.title | title }}
post.title = “a wonderFul tRip”
A WONDERFUL TRIP
a wonderful trip
A Wonderful Trip
Filters
I have {{ post_list.count }}
post{{ post_list.count |pluralize}}
post_list.count = 1
I have 2 postspost_list.count = 2
I have 1 post
Filters
{{ post.title|truncatewords:2 }}
{{ value|truncatechars:15 }}
django.contrib.humanize
1. Add 'django.contrib.humanize' to your INSTALLED_APPS settings
2. Use {% load humanize %} in a template
<!-- post.html -->
{% extends "base.html" %}
{% load humanize %}
{% block title %}{{ post.title }}{% endblock title %}
{% block content %}
<div class="post-heading">
<h1 class="title"><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title | title }}</a>
</h1>
<div class="date">{{ post.created_at|naturaltime }}</div>
</div>
…
{{ value|naturaltime }}
Philosophy
Express Presentation
Not Program Logic
Summary
• Template Inheritance:
• {% block %} {% end block %}
• {% extends %}
• {{ block.super }}
• Organize: {% include %}
• Comment: {# comment #}, {% comment %}{% endcomment %
• Filters
• upper , lower, title, 

pluralize, truncatewords, truncatechars
• django.contrib.humanize: naturaltime
Thank you

More Related Content

What's hot

PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation TechniqueMorshedul Arefin
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScriptShahDhruv21
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Chris Poteet
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom ManipulationMohammed Arif
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Modelchomas kandar
 
Css Complete Notes
Css Complete NotesCss Complete Notes
Css Complete NotesEPAM Systems
 
Html images syntax
Html images syntaxHtml images syntax
Html images syntaxJayjZens
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoKnoldus Inc.
 
javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Html and css presentation
Html and css presentationHtml and css presentation
Html and css presentationumesh patil
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial之宇 趙
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom eventBunlong Van
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSSAmit Tyagi
 

What's hot (20)

Django Models
Django ModelsDjango Models
Django Models
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Css Complete Notes
Css Complete NotesCss Complete Notes
Css Complete Notes
 
Html images syntax
Html images syntaxHtml images syntax
Html images syntax
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Html forms
Html formsHtml forms
Html forms
 
Html
HtmlHtml
Html
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Html and css presentation
Html and css presentationHtml and css presentation
Html and css presentation
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom event
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 

Similar to Django Template Inheritance and Filters

引き出しとしてのDjango - SoozyCon7
引き出しとしてのDjango - SoozyCon7引き出しとしてのDjango - SoozyCon7
引き出しとしてのDjango - SoozyCon7makoto tsuyuki
 
Templates81 special document
Templates81 special documentTemplates81 special document
Templates81 special documentLan Nguyen
 
Templates81 special document
Templates81 special documentTemplates81 special document
Templates81 special documentLan Nguyen
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersJames Gray
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Eric Palakovich Carr
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Joao Lucas Santana
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
 
`From Prototype to Drupal` by Andrew Ivasiv
`From Prototype to Drupal` by Andrew Ivasiv`From Prototype to Drupal` by Andrew Ivasiv
`From Prototype to Drupal` by Andrew IvasivLemberg Solutions
 
Django workshop : let's make a blog
Django workshop : let's make a blogDjango workshop : let's make a blog
Django workshop : let's make a blogPierre Sudron
 
Private slideshow
Private slideshowPrivate slideshow
Private slideshowsblackman
 
El Tiempo Nos Ensea 214392
El Tiempo Nos Ensea 214392El Tiempo Nos Ensea 214392
El Tiempo Nos Ensea 214392guestc65f09
 
Los Estados De La Materia
Los Estados De La MateriaLos Estados De La Materia
Los Estados De La MateriaMayritalinda
 
Intro to Jinja2 Templates - San Francisco Flask Meetup
Intro to Jinja2 Templates - San Francisco Flask MeetupIntro to Jinja2 Templates - San Francisco Flask Meetup
Intro to Jinja2 Templates - San Francisco Flask MeetupAlan Hamlett
 

Similar to Django Template Inheritance and Filters (20)

引き出しとしてのDjango - SoozyCon7
引き出しとしてのDjango - SoozyCon7引き出しとしてのDjango - SoozyCon7
引き出しとしてのDjango - SoozyCon7
 
Frfrfrf
FrfrfrfFrfrfrf
Frfrfrf
 
Templates81 special document
Templates81 special documentTemplates81 special document
Templates81 special document
 
Templates81 special document
Templates81 special documentTemplates81 special document
Templates81 special document
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and Controllers
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
 
`From Prototype to Drupal` by Andrew Ivasiv
`From Prototype to Drupal` by Andrew Ivasiv`From Prototype to Drupal` by Andrew Ivasiv
`From Prototype to Drupal` by Andrew Ivasiv
 
Django workshop : let's make a blog
Django workshop : let's make a blogDjango workshop : let's make a blog
Django workshop : let's make a blog
 
Private slideshow
Private slideshowPrivate slideshow
Private slideshow
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
El Tiempo Nos Ensea 214392
El Tiempo Nos Ensea 214392El Tiempo Nos Ensea 214392
El Tiempo Nos Ensea 214392
 
Los Estados De La Materia
Los Estados De La MateriaLos Estados De La Materia
Los Estados De La Materia
 
Articulo java web
Articulo java webArticulo java web
Articulo java web
 
smoke1272528461
smoke1272528461smoke1272528461
smoke1272528461
 
Intro to Jinja2 Templates - San Francisco Flask Meetup
Intro to Jinja2 Templates - San Francisco Flask MeetupIntro to Jinja2 Templates - San Francisco Flask Meetup
Intro to Jinja2 Templates - San Francisco Flask Meetup
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Django Template Inheritance and Filters

  • 2. What We Have Known {{ <post.title> }} {% if post.photo %} <div class="thumbnail"> <img src="{{ post.photo }}" alt=""> </div> {% else %} <div class="thumbnail thumbnail-default"></div> {% endif %} {% for post in post_list %} … {% endfor %} {{ post.created_at|date:"Y / m / d" }} Variable Tag <a href="{% url 'post_detail' pk=post.pk %}"> Filter
  • 6. <!-- base.html --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>A Django Girl's Adventure</title> <link href=“//…/assets/css/style.css” rel="stylesheet"> </head> <body> <div class="header"> <h1 class="site-title text-center"> <a href="/">A Django Girl’s Adventure</a> </h1> </div> <div class=“container"> {% block content %} {% endblock content %} </div> </body> </html>
  • 7. Extends a template <!-- home.html --> {% extends "base.html" %} {% block content %} {% for post in post_list %} <div class="post-wrapper"> <div class="post"> … </div> </div> {% endfor %} {% endblock content %}
  • 8. Extends a template {% extends "base.html" %} {% block content %} {% for post in post_list %} <div class="post-wrapper"> <div class="post"> … </div> </div> {% endfor %} {% endblock content %} {% extends %} must be the first template tag
  • 9. Fill the Block {% extends "base.html" %} {% block content %} {% for post in post_list %} <div class="post-wrapper"> <div class="post"> … </div> </div> {% endfor %} {% endblock content %} base.html Fill content to the block you defined
  • 10. <!-- base.html --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>A Django Girl's Adventure</title> <link href=“//…/assets/css/style.css” rel="stylesheet"> </head> <body> <div class=“header"> <h1 class="site-title text-center"> <a href="/">A Django Girl’s Adventure</a> </h1> <ul class="nav navbar-nav"> <li><a href="#">About Me</a></li> <li><a href="#">Favorite Trips</a></li> <li><a href="#">Albums</a></li> ... <li><a href="#">Contact Me</a></li> </ul> </div> <div class=“container"> {% block content %} {% endblock content %} </div> </body> </html> Include
  • 11. <!-- base.html --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>A Django Girl's Adventure</title> <link href=“//…/assets/css/style.css” rel="stylesheet"> </head> <body> <div class=“header"> <h1 class="site-title text-center"> <a href="/">A Django Girl’s Adventure</a> </h1> <ul class="nav navbar-nav"> <li><a href="#">About Me</a></li> <li><a href="#">Favorite Trips</a></li> <li><a href="#">Albums</a></li> ... <li><a href="#">Contact Me</a></li> </ul> </div> <div class=“container"> {% block content %} {% endblock content %} </div> </body> </html> Extract as Another Template
  • 12. <!-- base.html --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>A Django Girl's Adventure</title> <link href=“//…/assets/css/style.css” rel="stylesheet"> </head> <body> {% include ‘_header.html’ %} <div class=“container"> {% block content %} {% endblock content %} </div> </body> </html> Include Template
  • 13. Example: base.html <!DOCTYPE html> {% load static %} <html> <head> <link rel="stylesheet" href="main.css" /> {% block extra_css %}{% endblock extra_css %} <title> {% block title_tag %} {% block title %} Welcome {% endblock title %} | A Django Girl's Adventure {% endblock title_tag %} </title> </head> <body> {% include "_header.html" %} <div class="container"> {% block content %} {% endblock content %} </div> <script src="{% static 'js/vendors/jquery.js' %}"></script> <script src="{% static 'js/vendors/bootstrap.js' %}"></script> {% block extra_js %}{% endblock extra_js %} </body> </html>
  • 14. <!-- home.html --> {% block title %}Home{% endblock title %} <!-- post.html --> {% block title %}{{ post.title }}{% endblock title %} <title> {% block title_tag %} {% block title %} Welcome {% endblock title %} | A Django Girl's Adventure {% endblock title_tag %} </title> <!-- post.html --> {% block title_tag %}{{ post.title }}{% endblock title_tag %} Home | A Django Girl's Adventure | A Django Girl's Adventure
  • 15. block.super | Welcome | A Django Girl's Adventure <!-- post.html --> {% block title %}{{ post.title }} | {{ block.super }}{% endblock title %} <title> {% block title_tag %} {% block title %} Welcome {% endblock title %} | A Django Girl's Adventure {% endblock title_tag %} </title> Use {{ block.super }} to include parent’s content
  • 16. Common Conventions • Prefer underscore over dash in template name, block name • Included name of the block in endblock tag
 • Template called by other template are prefixed with `_` {% block content %} {% endblock content %} {% include ‘_header.html’ %} {% block title_tag %}
  • 17. 2-Tier Template Architecture templates/ base.html home.html # extends base.html trips/ trips_list.html # extends base.html trips_detail.html # extends base.html Best for websites with a consistent overall layout from app to app
  • 18. 3-Tier Template Architecture templates/ base.html trips/ base_trips.html # extends base.html trips_list.html # extends base_trips.html trips_detail.html # extends base_trips.html album/ base_album.html # extends base.html album_list.html # extends base_album.html album_detail.html # extends base_album.html Best for websites where each sections requires a distinctive layout
  • 19. Flat is Better Than Nested Zen of Python
  • 21. Comment in Django Template {% comment %} <div class="sidebar"> sidebar content is not ready </div> {% endcomment %} Multi-line Comment {# single line comment #} Single Line Comment <!-- HTML Comment -->
  • 23. Filters {{ post.title | upper }} {{ post.title | lower }} {{ post.title | title }} post.title = “a wonderFul tRip” A WONDERFUL TRIP a wonderful trip A Wonderful Trip
  • 24. Filters I have {{ post_list.count }} post{{ post_list.count |pluralize}} post_list.count = 1 I have 2 postspost_list.count = 2 I have 1 post
  • 25. Filters {{ post.title|truncatewords:2 }} {{ value|truncatechars:15 }}
  • 26. django.contrib.humanize 1. Add 'django.contrib.humanize' to your INSTALLED_APPS settings 2. Use {% load humanize %} in a template <!-- post.html --> {% extends "base.html" %} {% load humanize %} {% block title %}{{ post.title }}{% endblock title %} {% block content %} <div class="post-heading"> <h1 class="title"><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title | title }}</a> </h1> <div class="date">{{ post.created_at|naturaltime }}</div> </div> … {{ value|naturaltime }}
  • 28. Summary • Template Inheritance: • {% block %} {% end block %} • {% extends %} • {{ block.super }} • Organize: {% include %} • Comment: {# comment #}, {% comment %}{% endcomment % • Filters • upper , lower, title, 
 pluralize, truncatewords, truncatechars • django.contrib.humanize: naturaltime