Template application example
HelloWorld/ |-- HelloWorld | |-- __init__.py | |-- settings.py | |-- urls.py | |-- view.py | |-- wsgi.py |-- manage.py `-- templates `-- hello.html
Django template file path description:
Modify HelloWorld/settings.py, modify DIRS in TEMPLATES to [ os.path.join(BASE_DIR,'templates') ]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
The hello.html file code is as follows:
<h1>{{ hello }}</h1> # hello is a variable, {{ hello }}
from django.shortcuts import render def hello(request): context = {} context['hello'] = 'Hello World!' return render(request, 'hello.html', context)
Use render to replace the HttpResponse used previously. render also takes a dictionary context as an argument. The key "hello" of the element in the context dictionary corresponds to the variable "{{ hello }}" in the template.
Django template tags
if / else basic syntax (supports nesting)
{% if condition1 %} ... display 1 {% elif condition2 %} ... display 2 {% else %} ... display 3 {% endif %}
The {% if %} tag accepts and , or or not keywords to judge multiple variables, or to negate variables ( not )
{% if athlete_list and coach_list %} Both athletes and coaches variables are available. {% endif %}
for basic syntax
<ul> {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} </ul>
Adding a reversed to the label causes the list to be iterated in reverse:
{% for athlete in athlete_list reversed %} ... {% endfor %}
{% for %} tags can be nested:
{% for athlete in athlete_list %} <h1>{{ athlete.name }}</h1> <ul> {% for sport in athlete.sports_played %} <li>{{ sport }}</li> {% endfor %} </ul> {% endfor %}
ifequal/ifnotequal tags
The {% ifequal %} tag compares two values, and when they are equal, displays all values between {% ifequal %} and {% endifequal %}.
The following example compares two template variables user and currentuser:
{% ifequal user currentuser %} <h1>Welcome!</h1> {% endifequal%}
Similar to {% if %}, {% ifequal %} supports optional {% else%} tags:
{% ifequal section 'sitenews' %} <h1>Site News</h1> {% else %} <h1>No News Here</h1> {% endifequal%}
Annotation label
Django comments use {# #} {#this is a comment #}
filter
Template filters can modify a variable before it is displayed. Filters use the pipe character: {{ name|lower }} # After the {{ name }} variable is processed by the filter lower, the document uppercase converts the text to lowercase. Filter pipes can be *socketed*, that is, the output of one filter pipe can be used as the input of the next pipe: {{ my_list|first|upper }} # Convert the first element to uppercase. Some filters have parameters, and filter parameters follow a colon and are always enclosed in double quotes: {{ bio|truncatewords:"30" }} # Will display the first 30 words of the variable bio. Other filters: addslashes : adds backslashes before any backslashes, single or double quotes. date : format a date or datetime object according to the specified format string parameter: {{ pub_date|date:"F j, Y" }} length : Returns the length of the variable.
include tag
The {% include %} tag allows the content of other templates to be included in the template.
{% include "nav.html" %} #includes the nav.html template: