Django का Template Engine
In this page:
<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 करता है।
उदाहरण: What Is the Django Template Engine?
{{ name }} and {{ unread_count }} are placeholders the engine replaces with real values from the view's context.
<!-- 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 नहीं करते।
उदाहरण: Two Kinds of Template Syntax
{{ username }} prints a value; {% if %}...{% endif %} controls whether the enclosed HTML renders at all.
<!-- 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 किया जाता है।
उदाहरण: 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.
# 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.
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. #}
- Python logic (loops, कई branches वाले complex conditionals) सीधे templates में लिखना बजाय पहले view में data prepare करने के।
- यह भूल जाना कि template syntax output के लिए double curly braces {{ }} उपयोग करता है लेकिन tags के लिए curly-brace-percent {% %}, और दोनों को mix up करना।
- 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 रखता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: