程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django framework - template layer

編輯:Python

List of articles

Chapter one django Installation and introduction

Chapter two django Based on using

The third chapter The routing layer

Chapter four A virtual environment 、django Version difference 、 View layer

The fifth chapter Formwork layer

Chapter six The model layer


List of articles

  • List of articles
  • Template comment syntax
  • One 、 Filter of template syntax
    • 1. Grammatical structure
    • 2. Common filters
  • Two 、 label
    • 1. Grammatical structure
    • 2.if Judge
    • 3.for loop
  • 3、 ... and 、 Custom filter 、 Custom tag 、inclusion_tag
    • 1. Custom filter
    • 2. Custom simple tags : Can receive any parameters
    • 3. Customize inclusion_tag
  • Four 、 Template import
  • 5、 ... and 、 Template inheritance


Template comment syntax

html notes :
html Its own comments can be seen in the browser developer tool

<!-- The comment -->

django Template syntax notes :
django The comments of template syntax will not be displayed in the browser developer tool

{# The comment #}

One 、 Filter of template syntax

1. Grammatical structure

{ { Data objects | Filter name : Parameters }} The filter can only transmit one additional parameter at most

2. Common filters

django Template syntax provides 60 Multiple filters

 <p> The length of the statistics :{
{ s1|length }}</p>
<p> Arithmetic addition or string addition :{
{ n1|add:111 }}、{
{ s1|add:'big baby' }}</p>
<p> Convert the number to the appropriate document unit of measurement :{
{ file_size|filesizeformat }}、{
{ file_size1|filesizeformat }}</p>
{# If b The value is false Will show : Later content #}
<p> Judge whether the Boolean value corresponding to the current data object is False:{
{ b|default:' The Boolean value corresponding to the previous value is False' }}、{
{ s1|default:' The Boolean value corresponding to the previous value is False' }}</p>
{# If b The value is none Will show : Later content #}
<p> Judge whether the Boolean value corresponding to the current data object is False:{
{ b|default_if_none:' The Boolean value corresponding to the previous value is None' }}、{
{ s1|default_if_none:' The Boolean value corresponding to the previous value is None' }}</p>
{# ctime Value is converted to date Time format after Corresponding year - month - Japan when - branch - second #}
<p> Time format :{
{ ctime|date:'Y-m-d H-i-s' }}</p>
<p> Index slice :{
{ s1|slice:'0:8' }}</p>
<p> Intercept the specified number of text according to the space :{
{ s2|truncatewords:5 }}、{
{ s1|truncatewords:1 }}</p>
<p> Intercept text according to the number of characters ( Contains three points ):{
{ s2|truncatechars:5 }}、{
{ s1|truncatechars:10 }}</p>
<p> Remove the specified character :{
{ info|cut:'|' }}</p>
{# safe You can match the background parameters with html The syntax is transformed into pages #}
<p> Whether to cancel the conversion :{
{ tag1 }}、{
{ tag1|safe }}、{
{ scripts1|safe }}</p>
{# You can also handle it first when the view function passes values , In this way, it can also be directly escaped into a page #}
from django.utils.safestring import mark_safe
ttt = '<a href="https://www.baidu.com"> Am I </a>'
res = mark_safe(ttt)
<p>{
{ res }}</p>

Two 、 label

1. Grammatical structure

{% name ...%}
{% end name %}

2.if Judge

 {% if Conditions 1 %}
<p> How do you do </p>
{% elif Conditions 2 %}
<p> How is he </p>
{% else %}
<p> Hello everyone </p>
{% endif %}

3.for loop

 {% for i in l1 %}
<p>{
{ i }}</p>
{% endfor %}

stay {% for %} Internal loop , You can visit a website called forloop Template variables for . This variable has several properties , They give you some information about the loop process .
{‘parentloop’: {}, ‘counter0’: 0, ‘counter’: 1, ‘revcounter’: 4, ‘revcounter0’: 3, ‘first’: True, ‘last’: False}

 for+if Other uses
{% for i in l1 %}
{% if forloop.first %} {# forloop.first value #}
<p> This is the first cycle </p>
{% elif forloop.last %}
<p> This is the last cycle </p>
{% else %}
<p> Intermediate cycle </p>
{% endif %}
{% empty %}
<p>for The loop object is empty Automatic execution </p>
{% endfor %}

3、 ... and 、 Custom filter 、 Custom tag 、inclusion_tag

1. Under the application, you need to create a file named templatetags Folder
2. Create a folder with any name in this folder py file
3. In the py You need to write two lines of fixed code in advance

from django import template
register = template.Library()

1. Custom filter

templatetags Self construction under the environment py file

@register.filter(is_safe=True)
def index(a, b):
return a + b
 Custom filter : Only two parameters can be received
{% load self-built py File name %}
{
{ n1|index:666 }}

2. Custom simple tags : Can receive any parameters

templatetags Self construction under the environment py file

@register.simple_tag(name='my_tag')
def func1(a, b, c, d):
return a + b + c + d
{% load self-built py File name %}
{% my_tag 1 2 3 4 %} # The parameters can be separated by spaces

3. Customize inclusion_tag

inclusion_tag Is a partial page , Create local pages that need to be used repeatedly as inclusion_tag It can reduce code redundancy , At the same time, it makes the code more readable

First create a templatetags Folder
Put an arbitrary name in it py The corresponding contents are as follows :

# mytag.py
@register.inclusion_tag(' Template style file ') # Register the corresponding tag, have access to name Specify the name of the call without specifying that the function name should prevail 
def func(n):
l = []
for i in range(1, n + 1):
l.append(f' The first {
i} page ')
return locals()

Set template style file

<ul>
{% for foo in l %}
<li>{
{ foo }}</li>
{% endfor %}
</ul>

Insert the corresponding customization where you need to use tag

{% load Customize tag file name %}
{% Function name or name Name Then pass on the reference ( If any ) %}
<!-- Example code -->
{% load mytag %}
{% func 10 %}

Four 、 Template import

It's similar to going to html The partial pages on the page are made in the form of modules Where you want to import directly, you can display

Usage mode

 {% include 'menu.html' %}

5、 ... and 、 Template inheritance

Object oriented inheritance : If you inherit a page, you can use all the resources on that page

There should be at least three areas on the template :
css Area 、 Content area 、js Area

After delimiting three areas, the sub page can have its own independent css、js、 Content

1. First in the template through block Delimit areas that can be modified in the future

 {% block content %}
<h1> Home page content </h1>
{% endblock %}

2. The child board inherits the template

 {% extends 'home.html' %}

3. Modify the designated area

 {% block content %}
<h1> Login content </h1>
{% endblock %}

4. Child pages can also reuse the content of the parent page

 {
{ block.super }}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved