Date: 2014.12.16 Speaker: Apua Last Update: 2014.12.16
TEMPLATE_DIRS = (os.path.join(BASE_DIR, "template"),)
INSTALLED_APPS = ('myproject.polls', 'myproject.music')
TEMPLATE_LOADERS = (
('django.template.loaders.cached.Loader', (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)),
)
{% extend "base.html" %}
{% block body %}
{{ block.super }}
{{ form_subtitle }}
<form method="post">
{% include form_template %}
</form>
<a href="{% url 'home' %}">Home</a>
{% endblock %}
{# Those are `context` objects #}
{% if display_inbox %}
{% with emails=user.emails.all %}
<p>You have {{ emails|lenght }} email(s)</p>
{% for email in emails %}
<p>{{ email.body }}</p>
{% endfor %}
{% endif %}
{# A filter `lower` #}
{% "ABC"|lower %}
{# A tag `autoescape` #}
{% autoescape %}
{{ obj }}
{% endautoescape %}
myapp/
|-- __init__.py
|-- admin.py
|-- models.py
|-- templatetags
| `-- myapp_tags.py
|-- tests.py
`-- views.py
Recommand:
Warning:
Don't name template tag libraries with the same name as app .
Don't rely on IDE to determine the name.
<app_name>_tags.py
{% extends "base.html" %}
{% load myapp_tags %}
# myapp/templatetags/myapp_tags.py
from django import template
register = template.Library()
@register.filter
def myfilter(val, arg): # limit one or two arguments
pass
return "altered_string"
{% extends "base.html" %}
{% load myapp_tags %}
{% val|myfilter:arg %}
# myapp/templatetags/myapp_tags.py
from django import template
register = template.Library()
@register.tag
def mytag(parser, token, *args, **kwargs):
pass
return template.Node()
{% extends "base.html" %}
{% load myapp_tags %}
{% mytag arg0 arg1 kwarg0="val0"|lower kwarg1=var %}
This part would be token.
{% endmytag %}
Filters:
Modifying the presentation of data.
It can be readily reused in REST APIs
and other output formats
Tags:
Only responsible for rendering of HTML.
{% comment %}
`user_list` generated via
User.object.all() or User.selet_related("flavors")
{% endcomment %}
{% for user in user_list %}
{{ user.name }} <br />
{{ user.flavor.title }} <br />
{{ user.flavor.scoops_remaining }} <br />
{% endfor %}
See chapter 16: Tradeoffs of Replacing Core Components
# setttings/local.py
TEMPLATE_STRING_IF_INVALID = "INVALID EXPRESSION: %s"
Liberal use of log statements and tests are helpful.
See chapter 20: Testing Stinks and Is a Waste of Money!
# from django.template.defaultfilters import (slugify,
# remove_tags)
from django.utils.text import slugify
from django.utils.html.remove_tags
# Anti-pattern
from django import template
template.add_to_builtins("myapp.templatetags.myapp_tags")