If tags:
The if tag is the same as the if statement in Python, but all tags need ({%%}) to wrap and close the tag. The following is an example:
18%} {% if age < < p > you are a minor, cannot enter net cafe < / p > {% elif age = = 18%} < p > welcome to < / p >} else {% % < p > moderate game < / p > {% endif %}Copy the code
For in Empty tag:
The basic operation is the same as above, when the traversal is empty, execute the operation in empty for.. in.. The following is an example of the continue and break keywords in:
< p > {% for the key and the value in person. The items %} {{key}}, {{value}} {% endfor %} < / p > < table > < thead > < tr > < td > < / td > serial number < td > title < / td > < / td > < td > the author price < / td > < td > < / tr > < thead > < tbody > {% for the book in books %} {% if forloop. First %} < tr style="background: red;" > {% elif forloop.last %} <tr style="background: pink;" > {% else %} <tr> {% endif %} <td>{{ forloop.counter0 }}</td> <td>{{ book.name }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </tbody> </table> <ul> {% for content in contents %} <li>{{ content }}</li> {% Empty %} <li> no comments </li> {% endfor %} </ul>Copy the code
Url tag
The URL tag gives the text a hyperlink as shown in the following example:
Views section:
from django.shortcuts import render from django.http import HttpResponse def index(request): return render(request,'index.html') def login(request): Return HttpResponse(text) def book(request): HttpResponse(text) def book(request): HttpResponse(text) def book(request): Def book_detail(request,book_id): return HttpResponse(' book_id ') def book_detail(request,book_id): Return HttpResponse(text) def movie(request): HttpResponse(text) def movie(request): Def city(request): return HttpResponse(HttpResponse)Copy the code
Urls part
from . import views
urlpatterns = [
path('',views.index,name='index'),
path('book/',views.book,name='book'),
path('movie/',views.movie,name='movie'),
path('city/',views.city,name='city'),
path('book/detail/<book_id>/',views.book_detail,name='detail'),
path('login/',views.login,name='login'),
]
Copy the code
HMTL template section
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .nav{ overflow: hidden; } .nav li{ float: left; list-style: none; margin: 0 20px; } < / style > < / head > < body > < ul class = "nav" > < li > < a href = "/" > home page < / a > < / li > < li > < a href = "{% url 'book' %}" > reading < / a > < / li > < li > < a href = "{% url 'movie' %}" > movies < / a > < / li > < li > < a href = "{% url 'city' %}" > b < / a > < / li > < li > < a href = "{% url 'detail' = '1' book_id %} "> the fire an article < / a > < / li > < li > < a href =" {% url 'login' %}? Next =/"> login </a> </li> </ul> </body> </ HTML >Copy the code
Add <a{% URL ‘unique ID’ %}> property </a Adds a hyperlink to the property to redirect to the page specified by that ID when clicked
Add parameters to the URL directly with the space parameter name =, the query string is added at the end
Common template filters
Function calls are not supported in DTL, so you can’t pass parameters to a function, which would be very limited, and a filter is really a function that can take an argument and process it.
Add filter
The Add filter will try to make the parameters passed in an integer and then return and, if not, will try to concatenate strings with and sequences
{{ '1'|add:'2' }}
Copy the code
The cut filter
The cut filter removes the specified string
{{'HELLO WORLD'|cut:' '}}
Copy the code
The date filters
The date filter applies formatting to the time series
{{ today|date:'Y/m/d H:i:s' }}
Copy the code
Template inheritance
Place code that needs to be reused into the same template and invoke extends inheritance as shown in the following example:
Outside the template
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .nav{ overflow: hidden; } .nav li{ float: left; list-style: none; margin: 0 20px; } < / style > < / head > < body > < header > < ul > < li > {{username}} < / li > < / ul > < ul class = "nav" > < li > < a href = "/" > home page < / a > < / li > < li > < a href = "{% url 'company' %}" > corporate < / a > < / li > < li > < a href = "{% url 'school' %}" > school < / a > < / li > < / ul > < header > < div Class ="content"> {% block content %} This is the code in the parent template {% endBlock %} </div> <footer> this is the footer part </footer> </body> </ HTML >Copy the code
Subtemplate inheritance
{% extends 'bases.html' %}
Copy the code
For different pages, use the block tag to create an opening, give it a name, and then create it by calling the name in the child template, as shown in the following example:
Bases. HTML where the cut is made
<div class="content">
{% block content %}
{% endblock %}
</div>
Copy the code
Called in a child template
{% block content %} This is the home page code {% endblock %}Copy the code
This overrides the opening part of the parent template, so that the code in the parent template does not appear. If you want to use this code, like python class inheritance, you can call it with block.super, as shown in the following example:
{% block content %} {{block.super}} this is the home page of the code {% endblock %}Copy the code
Loading a static file
In a web page, you need not only HTML skeleton, but also CSS style files, JS executable files, some images, etc. {% load static %} {% load static %} {% load static %}}
1. To ensure the django. Control the staticfiles has been added to the Settings. INSALLED_APP.
2. Make sure STATIC_URL is set in settings.py (request url for static file)
3. Create a file named static under the installed APP, create a folder with the same name as the current APP, and place the static file in the folder. (Avoid identification errors when static files with the same name appear in each app)
4. If static files are not linked to any app, add STATICFILES_DIRS to settings.py and DTL will look for static files in this list path. Such as:
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
Copy the code
5. Use the load tag in the template to load the static tag. For example, the stytle.css file to load in the project’s static folder is shown below
{% load static %}
<img src="{% static 'front/view.jpg' %}" alt="">
Copy the code
6. If you don’t want to load static every time you load a static file in your template, Can be added in the TEMPLATES in Settings/OPTIONS ‘builtins: [‘ diango. Templatetags. Static], this module can directly use the static tags.
7. If there is no django is added in the INSTALLD_APP. Contrib. Staticfiles. We need to manually map the STATIC file url to the static file path.