Detailed explanation of Django template filter and inheritance example

Time:2022-6-5
catalogue

Template filter

Definition: process the value of a variable when it is output

Function: you can change the output display of variables by using filters

Syntax:{{variable | filter: 'parameter value 1' | filter 2:'parameter value 2'...}}

Common filters

filter explain
lower Convert all strings to lowercase
upper Convert all strings to uppercase
safe By default, HTML escape is not performed for strings in variables
add:“n” Increase the value of calue by n
truncatechars:‘n’ If the number of characters exceeds the specified number, it will be truncated. The truncated string will end with a translatable ellipsis sequence (“…”)

Inheritance of templates

Template inheritance can reuse the contents of the parent template. The child template directly inherits all the contents of the parent template and can overwrite the response blocks in the parent template

Syntax (parent template):

  • Define the block label in the parent template
  • Identify which are allowed to be modified in the word module
  • Block tag: defined in the parent template and can be overwritten in the child template

Syntax (sub template):

Inherit template,extendsLabel, written on the first line of the template file

For example: {% extensions’ base.html’%}

The child template overrides the content block in the parent template

?
1
2
3
{% block block_name %}
Need to overwrite block_ Content of name block
{% endblock block_name %}   #Block at this time_ Name can be left blank

Template inheritance example

views.py

?
1
2
3
4
5
6
def base_view(request):
    return render(request,'base.html')
def music_view(request):
    return render(request,'music.html')
def sport_view(request)
    return render(request,'sport.html')

Write the corresponding HTML file in the Templates folder:

  • base.html
  • music.html
  • sport.html

base.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<head>
    <meta charset="utf-8">
    {% block mytitle %}
    <title>Home page</title>
    {% endblock mytitle%}
</head>
 
<body>
    <a href="/music_index">Music channel</a>
    <a href="/sport_index">Sports channel</a>
    <br>
    {% block info %}
    This is the home page
    {% endblock info %}
    <h3>If you have any questions, please contact XXXX</h3>
</body>
</html>

sport.html

?
1
2
3
4
5
6
{% extends 'base.html' %}
{% block mytitle %}
    <title>Sports channel</title>
{% block info %}
Welcome to the sports channel
{% endblock info %}

music.html

?
1
2
3
4
5
6
{% extends 'base.html' %}
{% block mytitle %}
    <title>Music channel</title>
{% block info %}
Welcome to the music channel
{% endblock info %}

At url Binding corresponding routes in PY

?
1
2
3
path('base_index',views.base_view)
path('music_index',views.music_view)
path('sport_index',views.sport_view)

test result

Detailed explanation of Django template filter and inheritance example
Detailed explanation of Django template filter and inheritance example
Detailed explanation of Django template filter and inheritance example

Note: when the template inherits, the server-side dynamic content cannot be inherited

summary

This is the end of this article on Django template filters and inheritance. For more information about Django template filters and inheritance, please search previous articles on developeppaper or continue to browse the following articles. I hope you will support developeppaper in the future!

Recommended Today

Numerical Analysis: Power Iteration and PageRank Algorithm (Numpy Implementation)

1. Power iterative algorithm (referred to as the power method) (1) Dominant eigenvalues ​​and dominant eigenvectors known square matrix\(\bm{A} \in \R^{n \times n}\), \(\bm{A}\)The dominant eigenvalue of is the ratio\(\bm{A}\)The other eigenvalues ​​of (the absolute value of) are all large eigenvalues\(\lambda\), if such an eigenvalue exists, then with\(\lambda\)The relevant eigenvectors are called dominant eigenvectors. (2) […]