MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Template Engine Basics0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
4.1Django Forms & Validation4.2Template Engine Basics4.3Template Inheritance
HomeTutorialsDjango for Backend DevelopmentTemplate Engine Basics
Template Inheritance

Template Engine Basics

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Django's built-in Django Template Language (DTL) is designed to balance power with ease of use. It bridges backend Python view logic and frontend HTML presentation. DTL enforces a strict separation of concerns: templates control presentation, while business logic remains in Python views and models.


1. DTL Syntax Building Blocks

Django templates use four primary syntactic constructs:

  1. 1
    Variables ({{ variable }}): Evaluates a variable from the context and outputs its string value.
  2. 2
    Tags ({% tag %}): Executes template logic, such as loops, conditional blocks, template inheritance, and URL resolution.
  3. 3
    Filters ({{ value|filter }}): Modifies variables prior to display (e.g. {{ title|title }}, {{ created_at|date:"M d, Y" }}).
  4. 4
    Comments ({# comment #}): Comments omitted from the final HTML output.

2. Template Variables & Dot Notation

DTL uses dot notation (.) to traverse objects, dictionaries, and methods:

1{# Context passed from view: {"article": article_obj, "stats": {"likes": 42}} #}
2 
3<h1>{{ article.title }}</h1>
4<p>Author: {{ article.author.get_full_name }}</p>
5<span>Likes: {{ stats.likes }}</span>

Lookup Priority Order:

When evaluating foo.bar, Django tries in order:

  1. 1
    Dictionary lookup (foo['bar'])
  2. 2
    Attribute lookup (foo.bar)
  3. 3
    Method call (foo.bar()) - Note: methods that require arguments cannot be called in DTL!
  4. 4
    List-index lookup (foo[bar])

3. Core Template Tags

Conditional Blocks (if, elif, else)

1{% if article.status == "published" %}
2 <span class="badge badge-success">Live</span>
3{% elif article.status == "draft" %}
4 <span class="badge badge-warning">Draft Mode</span>
5{% else %}
6 <span class="badge badge-secondary">Archived</span>
7{% endif %}

Looping with for and empty

1<div class="article-grid">
2 {% for article in articles %}
3 <article class="card">
4 <h2>{{ article.title }}</h2>
5 <p>{{ article.content|truncatewords:25 }}</p>
6 <small>Loop Index: {{ forloop.counter }} of {{ forloop.length }}</small>
7 </article>
8 {% empty %}
9 <p class="empty-state">No articles have been published yet.</p>
10 {% endfor %}
11</div>

The {% empty %} block executes automatically if the list is empty or undefined, eliminating boilerplate {% if articles %} wrapping!


4. Useful Built-in Filters

  • |lower / |upper / |title: String case formatting.
  • |truncatewords:30: Truncates text after 30 words with ....
  • |date:"F j, Y, g:i a": Formats dates (e.g. "September 15, 2026, 4:45 am").
  • |default:"N/A": Fallback if variable is false, empty, or None.
  • |length: Returns the count of items in an array or string.

5. Reverse URL Resolution with {% url %}

Never hardcode relative URLs (/articles/5/) in HTML templates. Use the {% url %} tag to dynamically generate URLs from view names:

1{# Resolves URL defined as path("<slug:slug>/", views.detail, name="detail") in articles app #}
2<a href="{% url 'articles:detail' slug=article.slug %}">
3 Read Full Story
4</a>

If you ever change your URL routing structure in urls.py, all template links update automatically without broken links!


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What syntax does the Django Template Language use to output a variable value?
Question 2
What does the {% empty %} clause inside a {% for %} loop accomplish?
Question 3
Why can't you call Python methods that require arguments inside standard DTL templates (e.g. {{ article.calculate_cost(5) }})?
Question 4
What filter formats long text down to 20 words followed by an ellipsis?
Question 5
Why is using {% url 'articles:detail' slug=article.slug %} superior to writing href="/articles/{{ article.slug }}/"?
Next Lesson

Template Inheritance

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Django Forms & ValidationTemplate Inheritance

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext