Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 1 of 10 · Django Templates
Interview question

What is the Django Template Language (DTL) and how does it differ from plain Python? Django Template Language (DTL) क्या है और plain Python से कैसे अलग है?

Answer

DTL is a restricted, designer-friendly templating syntax embedded in HTML files, intentionally limited so templates stay simple and separated from complex business logic.

# book_list.html
<h1>Books</h1>
<ul>
{% for book in books %}
    <li>{{ book.title }} - ${{ book.price }}</li>
{% empty %}
    <li>No books available</li>
{% endfor %}
</ul>

{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}!</p>
{% else %}
    <p>Please log in</p>
{% endif %}
DTL restrictionWhy it exists
No arbitrary Python expressions ({{ x + y }} works only for simple cases via filters)Keeps templates safe for non-programmers (designers) to edit
No function calls with arguments directlyPrevents complex logic from leaking into presentation layer
Dot notation (.) accesses attributes, dict keys, AND method calls uniformlySimplifies syntax - {{ obj.attr }} works whether attr is a field, method, or dict key
# {{ book.title }} tries, in order:
# 1. book['title'] (dictionary lookup)
# 2. book.title (attribute lookup)
# 3. book.title() (method call, if title is callable)
# 4. book[0] (numeric index, if title is a number)

# Rendering a template from a view
from django.shortcuts import render
def book_list(request):
    books = Book.objects.all()
    return render(request, 'book_list.html', {'books': books, 'page_title': 'All Books'})

DTL एक restricted, designer-friendly templating syntax है जो HTML files में embedded होता है, जानबूझकर limited है ताकि templates simple रहें और complex business logic से अलग हों।

# book_list.html
<h1>Books</h1>
<ul>
{% for book in books %}
    <li>{{ book.title }} - ${{ book.price }}</li>
{% empty %}
    <li>कोई books उपलब्ध नहीं</li>
{% endfor %}
</ul>

{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}!</p>
{% else %}
    <p>कृपया login करें</p>
{% endif %}
DTL restrictionक्यों है
Arbitrary Python expressions नहींTemplates को designers के लिए safe रखता है
Function calls सीधे arguments के साथ नहींComplex logic presentation layer में नहीं आती
Dot notation (.) attributes, dict keys, method calls सबके लिएSyntax simplify करता है
# {{ book.title }} इस order में try करता है:
# 1. book['title']
# 2. book.title
# 3. book.title() (callable हो तो)
# 4. book[0]

from django.shortcuts import render
def book_list(request):
    books = Book.objects.all()
    return render(request, 'book_list.html', {'books': books, 'page_title': 'All Books'})

Was this answer clear?