All technological notes.
template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.Two main ways to organize template structure in Django:
When Django loads template, it will look for the TEMPLATES variable in the settings.py
DIRS key is an empty list and the APP_DIRS is True. Then Djanto will look for templates folder within each app foler.DIRS key is a list of paths, Django will look for templates folder in those paths. No matter the value of APP_DIRS.DIRS key is an empty list and APP_DIRS is False, django will return a TemplateDoesNotExist error.The default Django template loader will try to load the template from the project-level directory first. In other words, DIRS is searched before APP_DIRS.
But to avoid namespace issues, a folder with the app name should be created below the app level template folder.
Approach
template.<app_name> under the the template folder.<app_name> folderStrucure:
manage.py<proj_name>/: folder for project
__init__.pysettings.pyurls.pywsgi.py<app_name>/: folder for application
__init__.pyadmin.pyapps.pymodels.pytests.pyviews.pytemplates/: folder for templates
<app_name>/: folder repeating app name to prevent namespace issues
.html: html filesTEMPLATES variable in settings.py
TEMPLATES = [
{
'DIRS': [], # by default, DIRS is an empty list
'APP_DIRS': True, # True when settings.py file is created. But default value is False.
},
]
It’s often more convenient to have all the templates in one place rather than hunting for them within multiple apps.
Settings.py: Update the DIRS config under TEMPLATES.
# settings.py
TEMPLATES = [
{
'DIRS': [Path(BASE_DIR, 'templates'),], # locate the templates folder under the BASE_DIR.
'APP_DIRS': True,
},
]
Strucure:
manage.py
<proj_name>/: folder for project
__init__.pysettings.pyurls.pywsgi.py<app_name>/: folder for application
__init__.pyadmin.pyapps.pymodels.pytests.pyviews.pytemplates/: folder for templates
<app_name>/: folder repeating app name to prevent namespace issues
.html: html filesTemplateDoesNotExist at /:
settings.py
INSTALLED_APPS does not include the applicationAPP_DIRS key of TEMPLATES is False, but the DIRS key is empty list or incorrect.view.py
template_name value in render function incorrect.A Django template is a text document or a Python string marked-up using the Django template language.
A template is rendered with a context. Rendering replaces variables with their values, which are looked up in the context, and executes tags. Everything else is output as is.
The syntax of the Django template language involves four constructs:
variable:
Variables are surrounded by `` like this:
Examples:
Tags
Tags are surrounded by { % % } like this
Examples:
__ csrf_token __
__ cycle 'odd' 'even' __ <!-- accept arguments -->
<!-- if -->
__ if user.is_authenticated __
Hello, .
__ endif __
Filters:
Examples:
<!-- take an argument -->
CommentsA __ comment __ tag provides multi-line comments.
{# this won't be rendered #}
HTML escaping:
For example, < less than symbol > has a special meaning in HTML markup language. HTML escaping will convert a string to HTML entities or convert HTML entities to plain text.
When storing raw HTML in databases or variables, special characters must be escaped so that they are not confused for markup.
By default in Django, every template automatically escapes the output of every variable tag.
Turn off automatic HTML escaping:
For individual variables:
safe filter.This will not be escaped:
For template blocks:
autoescape off tag.Auto-escaping is on by default. Hello __ autoescape off __ This
will not be auto-escaped: . Nor this: __
autoescape on __ Auto-escaping applies again: __ endautoescape __
__ endautoescape __
Syntax:
object.all: calls QuerySet .all()methodobject.fk_set.all: calls .all()method on a collection of objects related on a foreign keyobject.all.count: call .all().count() methodobject.func: call a custom function defined in the models class.it is not possible to pass arguments to method calls accessed from within templates