[Django]-What does "load" do in Django Templates?

6πŸ‘

βœ…

β€œweblog” after β€œload” (in template file django_website/templates/base_weblog.html) refers to file weblog.py in folder django_website/apps/blog/templatetags. Folder templatetags must be named exactly that and must contain a file named __init__.py (question 2).

β€œload” makes the custom template tags (render_latest_blog_entries and render_month_links in this case) available for use in templates, django_website\templates\base_weblog.html in this case. β€œLoad” is a security and performance function.

15πŸ‘

load:

Load a custom template tag set.

See Custom tag and filter libraries for more information.

0πŸ‘

load tag can load built-in and custom Django template tags and filters by setting the files in load tag.

For example of a custom Django template tag and filter, create templatetags folder with __init__.py(Empty file) and custom_tags.py in core folder where settings.py is as shown below, then don’t forget to restart server to apply custom_tags.py to Django project. *Other names are fine for custom_tags.py and according to my experiments, custom_tags.py works without __init__.py(Empty file) but basically, it is better to create __init__.py(Empty file) just under templatetags folder by following what the doc says:

django-project
 |-core
 |  |-settings.py
 |  β””-templatetags # Here
 |     |-__init__.py
 |     β””-custom_tags.py
 |-templates
 |  β””-index.html
 |-app1
 β””-app2

And, don’t forget to set core to INSTALLED_APPS in settings.py to apply custom_tags.py to Django project as shown below. *Setting core to INSTALLED_APPS also can collect the static files in core folder to the root Django Project folder with python manage.py collectstatic. I recommend to set core to INSTALLED_APPS before you start building your Django Project:

# "core/settings.py"

INSTALLED_APPS = [
    # ...
    'core', # Here
    'app1',
    'app2',
]

Then, create uppercase and lowercase tags in custom_tags.py as shown below. *You can see my answer explaining @register.simple_tag:

# "core/templatetags/custom_tags.py"

from django.template import Library

register = Library()

@register.simple_tag
def uppercase(arg):
    return arg.upper()

@register.filter
def lowercase(arg):
    return arg.lower()

Then, you need to set the file custom_tags by omitting .py in load tag to use uppercase and lowercase tags as shown below:

# "templates/index.html"

{% load custom_tags %}

{% uppercase "Hello World" %} # HELLO WORLD
{{ "Hello World" | lowercase }} # hello world

And, you can also create the folders like app1 and app2 with __init__.py(Empty file) and custom_tags.py in templatetags folder as shown below. *Other names are fine for app1 and app2 folders and according to my experiments, custom_tags.py in app1 and app2 folders doesn’t work without __init__.py(Empty file) so __init__.py(Empty file) must be created just under app1 and app2 folders:

django-project
 |-core
 |  |-settings.py
 |  β””-templatetags
 |     |-app1 # Here
 |     |  |-__init__.py
 |     |  β””-custom_tags.py
 |     |-app2 # Here
 |     |  |-__init__.py
 |     |  β””-custom_tags.py
 |     |-__init__.py
 |     β””-custom_tags.py
 |-templates
 |  β””-index.html
 |-app1
 β””-app2

Then, you need to set the file custom_tags following the folders app1 and app2 in load tag to use the tags defined in app1/custom_tags.py and app2/custom_tags.py as shown below:

# "templates/index.html"
                   #  ↓ ↓ Here ↓ ↓     ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}

# ...

And, if there are the same tags like uppercase through multiple files as shown below:

# "core/templatetags/custom_tags.py"

@register.simple_tag
def uppercase(arg):
    return arg.upper()
# "core/templatetags/app1/custom_tags.py"

@register.simple_tag
def uppercase(arg):
    return arg.upper() + " app1"
# "core/templatetags/app2/custom_tags.py"

@register.simple_tag
def uppercase(arg):
    return arg.upper() + " app2"

Then, the uppercase tag of the last file set in load tag is used

# "templates/index.html"
                                    #  ↓ ↓ Here ↓ ↓
{% load custom_tags app1.custom_tags app2.custom_tags %}

{% uppercase "Hello World" %} # HELLO WORLD app2

Lastly, you can create multiple templatetags folders through multiple app folders app1 and app2 in addition to core folder as shown below:

django-project
 |-core
 |  |-settings.py
 |  β””-templatetags # Here
 |     |-__init__.py
 |     β””-custom_tags.py
 |-templates
 |  β””-index.html
 |-app1
 |  β””-templatetags # Here
 |     |-__init__.py
 |     β””-custom_tags.py
 β””-app2
    β””-templatetags # Here
       |-__init__.py
       β””-custom_tags.py

However, I recommend to create only one templatetags folder in core folder because there are 3 reasons as shown below:

  1. It is easier to manage only one templatetags folder in core folder than managing multiple templatetags folders through multiple app folders.

  2. For Django Admin, there is no folder to create templatetags folder so core folder is the most reasonable place to do it for Django Admin.

And, the 3rd reason is because through multiple templatetags folders, if there are multiple same name files like custom_tags.py as shown below:

django-project
 |-core
 |  |-settings.py
 |  β””-templatetags
 |     |-__init__.py
 |     β””-custom_tags.py # Here
 |-templates
 |  β””-index.html
 |-app1
 |  β””-templatetags
 |     |-__init__.py
 |     β””-custom_tags.py # Here
 β””-app2
    β””-templatetags # Here
       |-__init__.py
       β””-custom_tags.py # Here

Then, only one file is loaded in Django Templates but I don’t know which file to be loaded according to my experiments as shown below:

# "templates/index.html"
                             
{% load custom_tags %} # I don't know which `custom_tags.py` is loaded. 

Leave a comment