← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 1 of 10

Django का Template Engine

Django templates fill-in-the-blank worksheets जैसे हैं: आप HTML एक बार लिखते हैं, और Django हर बार page load होने पर real data से blanks भरता है।
Syntax
markup
<h1>{{ variable_name }}</h1>

{% tag_name %}
  ...
{% endtag_name %}

Django Template Engine क्या है?

Template engine special syntax वाली एक HTML file और data का एक Python dictionary (वह context) लेता है, और placeholders को real values से substitute करके एक final HTML string produce करता है।

Note: Template files सिर्फ .html files हैं जो आपके app के अंदर एक templates/ folder में stored हैं।

उदाहरण: What Is the Django Template Engine?

{{ name }} and {{ unread_count }} are placeholders the engine replaces with real values from the view's context.

markup
<!-- templates/greeting.html -->
<!DOCTYPE html>
<html>
<body>
  <h1>Hello, {{ name }}!</h1>
  <p>You have {{ unread_count }} new messages.</p>
</body>
</html>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Template Syntax के दो प्रकार

Double braces {{ variable }} एक value output करते हैं। Curly-brace-percent {% tag %} loops, conditionals, और includes जैसे template tags run करते हैं -- वे सीधे text output नहीं करते।

Warning: {{ }} और {% %} को mix up करना beginners के लिए सबसे common single Django template error है।

उदाहरण: Two Kinds of Template Syntax

{{ username }} prints a value; {% if %}...{% endif %} controls whether the enclosed HTML renders at all.

markup
<!-- output a variable -->
<p>{{ username }}</p>

<!-- run a tag (no output by itself) -->
{% if is_logged_in %}
  <p>Welcome back!</p>
{% endif %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Django आपके Templates कैसे ढूंढता है

By default, Django हर installed app के अंदर एक templates/ folder ढूंढता है। myapp/templates/myapp/page.html पर एक template code में 'myapp/page.html' के रूप में reference किया जाता है।

Note: Templates को एक app-named subfolder (templates/myapp/...) के अंदर namespace करना apps के बीच name clashes से बचाता है।
Warning: अगर TEMPLATES loaders आपकी file नहीं ढूंढ पाते, तो Django उन exact paths के साथ TemplateDoesNotExist raise करता है जो उसने search किए।

उदाहरण: How Django Finds Your Templates

render() looks for myapp/page.html inside myapp/templates/ — the 'myapp/' prefix inside templates/ prevents name clashes when two different apps both have a page.html.

markup
# myapp/views.py
from django.shortcuts import render

def show_page(request):
    return render(request, 'myapp/page.html', {})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

settings.py में Template Engine

TEMPLATES setting configure करती है कि कौन सा backend आपके templates render करता है और यह उन्हें कहां ढूंढता है, including APP_DIRS option जो per-app templates/ folder convention enable करता है।

उदाहरण: The Template Engine in settings.py

APP_DIRS=True tells Django to automatically search every installed app's templates/ folder.

markup
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
            ],
        },
    },
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Python logic (loops, कई branches वाले complex conditionals) सीधे templates में लिखना बजाय पहले view में data prepare करने के।
  2. यह भूल जाना कि template syntax output के लिए double curly braces {{ }} उपयोग करता है लेकिन tags के लिए curly-brace-percent {% %}, और दोनों को mix up करना।
  3. App की templates/ directory के बाहर .html template files रखना ताकि Django का template loader उन्हें न ढूंढ पाए।
चैप्टर सारांश
  • Django Template Language (DTL) आपको {{ }} और {% %} syntax उपयोग करके HTML के अंदर variables और simple logic embed करने देती है।
  • Templates plain text files हैं जो Django के template engine द्वारा render होती हैं, जो placeholders को real context data से replace करता है।
  • Logic को templates से बाहर और views/models में रखना templates को simple और readable रखता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.