L06-Django Template Language
L06-Django Template Language
Updates
https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html
Django
Model-View-Template (MVT)
Design pattern which also decouples user-interface (template), data
(model), and logic (view)
applies template
● URL dispatcher is part of
MVC controller
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Home_page
Django Template Language
DTL supports imperative programming features
Data passed from view to template via context
Variables are replaced by data from context
Tags control the logic of the template
Data passed from view to template via context
Members variable, dictionary lookup, index access all use dot operator
● {{user_list.0}}
● {{request.POST.username}}
{% block content %}
{% for entry in blog_entries %}
<h2>{{ entry.title }}</h2>
<p>{{ entry.body }}</p>
{% endfor %}
{% endblock %}
The sidebar block wasn’t defined…
what do you think will happen as a
result?
Root template folder
Typically, each template belongs to only one view and should be
placed inside their respective app’s folder
Some templates have common components across apps (e.g.,
navigation bar, footer, form elements, etc.)
Reusable templates can be placed in a root template directory
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [ BASE_DIR / "templates" ],
'APP_DIRS': True,
'OPTIONS': {
…
},
Add this line to the project’s settings.py file
},
]
Include tag
The include tag allows you to include a sub-template (i.e., block of
content that is the same for many pages) inside the current
template.
Syntax: {% include template_name %}
Can pass additional context;
e.g., {% include"greeting.html" with person="Bob" %}
Can restrict context to only ones explicitly passed in;
e.g., {% include"greeting.html" with person="Bob" user=user only%}
Reproduction and/or sharing of course materials is prohibited. Course materials include lecture slides, course notes,
assignments, data and documents provided by the instructors. Course materials created by the instructors of this course
are their intellectual properties. They may not be shared, posted, rehosted, sold, or otherwise distributed and/or
modified without expressed permission from the authors. All such reproduction or dissemination is an infringement of
copyright and is prohibited. All rights are reserved by the instructors.