Template language for the Django framework

Source: Internet
Author: User
Tags truncated

Browse Catalogs
    • Label
    • Filter filters

First, label TAGS1, common variables
    • Normal variable with {{}}
    • Variable names consist of numbers, letters, underscores
    • Point. Used in template language to get the corresponding property value of an object

Example:

{# takes the first argument in variable #} {{variable.0}} {# Take the value of key in the Dictionary dic #} {{Dic.key}} {# takes the Attr property value of the first object in the Obj_list object list #} {{obj_list.0.attr}} {# Point operation can only invoke a method with no parameters #} {{Obj_list.0.method}}
2. Logical Judgment

Logical judgment with {%%}

For
    • Syntax:{% for item in item_list%} ... {% empty%} ... {% ENDFOR%}
    • function: Loop item_list, if the item value is empty or not present, the code in empty is executed, remember to write ENDfor end loop
    • Common for Loop parameters:

Variable Description
Forloop.counter Index value of the current loop (starting at 1)
Forloop.counter0 Index value of the current loop (starting at 0
Forloop.revcounter Reverse index value of the current loop (starting at 1)
Forloop.revcounter0 Reverse index value of the current loop (starting at 0)
Forloop.first The current loop is not the first loop (Boolean value)
Forloop.last The current loop is not the last loop (Boolean value)
Forloop.parentloop Outer loop of this layer loop
If
    • Grammar:
      {% if some_condition%} ... {% elif other_condition%} ... {% ENDIF%}
    • function: Conditional judgment, remember to write endif end condition judgment
    • Keywords commonly used in conjunction with IF statements
      {% if some_condition%} ... {% elif other_condition%} ... {% ENDIF%}
With
    • Grammar
{% with new_variable = old_variable%} ..... {% Endwith%}
    • Role: Define intermediate variables
Csrf_token
    • Syntax: Write on each form in the page

{% Csrf_token%}
    • Role: For cross-site request forgery protection

Comments

Grammar:

{# comment Content #}
3. Master Board System inheritance
<! DOCTYPE html>
    • Grammar
{% extends ' base.html '%}
    • Function: Use the above syntax to inherit the motherboard at the top of the page in a sub-page
Blocks block
    • Grammar
{% block Block_name%} ... {% Endblock%}
    • function: replace the corresponding content in the motherboard by defining the block name in the motherboard in the sub-page

Example:

{% block Page-main%}  <p> Thin </p>  <p> human evil </p>  <p> rain Send dusk flower </p>{% endblock%}
Component
    • Grammar
{% include ' subassembly.html '%}
    • Function: You can save commonly used fixed HTML content in a separate file, where needed to import using the above syntax
4. Static file Related

In Settings, you set up the static statically folder and import it in HTML.

Grammar:

{% load static%}

For example:

{% load static%}

When referencing a JS file, use:

{% load static%}<script src= "{% static" Mytest.js "%}" ></script>

A file is used in many places to be saved as a variable

{% load static%} {% static "images/hi.jpg" as Myphoto%}</img>
Using Get_static_prefix
{% load static%}

Or

{% load static%} {% Get_static_prefix as Static_prefix%}
5. Custom Simpletag
    • Function: Can customize the label, can be used after registration
    • Steps for customizing Simpletag ( similar to custom filter, but receiving more flexible parameters )

      • Create a folder named Templatetags in your project's app
      • Create a custom py file in the Templatetags folder, for example: custom_simpletag.py
      • Write custom Simpletag in custom_simpletag.py, for example:

From django Import templateregister = template. Library () @register. Simple_tag (name= "My_simpletag") def Add_simpletag (a,b,c): # can receive multiple parameters return ' {} + {} + {} '. Format (A, B, C
      • Before using custom Simple_tag, first import the py file in the HTML page
{% load Custom_simpletag%} {% my_simpletag "1" "2" "3"%}
6. Custom Inclusion_tag
    • Function: Return HTML code Snippet
    • Steps to customize Inclusion_tag

      • Create a folder named Templatetags in your project's app
      • Create a custom py file in the Templatetags folder, for example: custom_inclusiontag.py
      • Write custom Inclusiontag in custom_inclusiontag.py, for example:

From django Import templateregister = template. Library () @register. Inclusion_tag (' inclusion_tag.html ') def my_inclusiontag (n): n = 1 if n < 1 else int (n) data = ["{} key". Format (i) for I in range (1, n+1)]return {"Data": Data}
      • Create the inclusion_tag.html file that you just registered in the Templates folder
<ul>  {% for choice in data%}  <li>{{choice}}</li>  {% endfor%}</ul>
      • Before using custom My_inclusiontag, first import the py file in the HTML page
{% load Custom_inclusiontag%} {% My_inclusiontag 10%}
Second, filter Filter1, built-in filter

Grammar:

{{Value|filter_name:args}}

Note: There are no spaces before and after pipe characters

Default
    • Grammar:
{{value:default: ' Custom Content '}}
    • Function: Displays the custom content if value is not passed
Length
    • Grammar:
{{Value|length}}
    • Function: Returns the length of value, such as value=[' A ', ' B ', ' C ', ' d '], which shows 4.
Filesizeformat
    • Grammar:
{{Value|filesizeformat}}
    • Function: Format value as a "human readable" file size format

If value is 123456789, the output will be 117.7 MB.

Slice
    • Grammar:
{{value|slice: ' Start:end '}}
    • Function: Slicing
Date
    • Grammar:
{{value|date: "Y-m-d h:i:s"}}
    • Function: Custom format value for time format
Safe
    • Grammar:
{{Value|safe}}
    • Function: Cancels the automatic escape of Django syntax tags such as HTML tags and js, thinking that inserting HTML or JS is safe and does not have to be escaped
Truncatechars
    • Grammar:
{{Value|truncatechars:num}}
    • Action: If the string character is more than the specified number of characters, it is truncated. The truncated string ends with a translatable sequence of ellipses ("..."). Num parameter refers to the number of characters truncated
2. Custom Filter Steps
    • Create a folder named Templatetags in your project's app
    • Create a custom py file in the Templatetags folder, for example: custom_filter.py
    • Write a custom filter in custom_filter.py, for example:

From django Import templateregister = template. Library () @register. Filter (name= "My_filter") def my_filter (value, arg): #最多接收两个参数return value.replace (ARG, "")
    • Before using a custom filter, first import the py file in the HTML page
{% load Custom_filter%} {{variable|my_filter: ' 0 '}}

  

 

Template language for the Django framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.