Template layer of Python--django

Source: Internet
Author: User
Tags truncated

1. Function of the template layer

In order to more logically render the data in the database into a template.

2. Template syntax:

{{Var_name}} #渲染变量用的

{% URL%} #渲染标签用的

3. Rendering of variables

Example:

def index (Request):    import datetime    s= "Hello"    l=[111,222,333]    # list    dic={"name": "Yuan", "age" : +}  # dictionary date    = Datetime.date (1993, 5, 2)   # Date Object     class Person (object):        def __init__ (self,name): C11/>self.name=name     Person_yuan=person ("Yuan")  # Custom Class object    Person_egon=person ("Egon")    Person_ Alex=person ("Alex")     Person_list=[person_yuan,person_egon,person_alex]      return render (Request, " Index.html ", {" L ": L," dic ":d IC," date ":d ate," Person_list ":p Erson_list})

4. Filter of the template

Grammar:

{{Obj|filter__name:param}}
default

If a variable is false or empty, the given default value is used. Otherwise, the value of the variable is used. For example:

{{value|default: "Nothing"}}
Length

Returns the length of the value. It works on both strings and lists. For example:

{{Value|length}}

If value is [' A ', ' B ', ' C ', ' d '], then the output is 4.

filesizeformat

Format the value as a "human-readable" file size (for example,, ‘13 KB‘ ‘4.1 MB‘ , and ‘102 bytes‘ so on). For example:

{{Value|filesizeformat}}

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

Date

If Value=datetime.datetime.now ()

{{value|date: ' y-m-d '}}
Slice

If value= "Hello World"

{{value|slice: ' 2:-1 '}}
Truncatechars

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 ("...").

Parameters: the number of characters to truncate

For example:

{{Value|truncatechars:9}}

If value is "Joel is a" , the output will be "Joel i ...".

Safe

The Django template automatically escapes syntax tags such as HTML tags and js, for obvious reasons, for security. But sometimes we may not want these HTML elements to be escaped, such as we do a content management system, the background is added to the article is decorated, these decorations may be a similar to the FCKeditor editor to fill the HTML modifier text, If auto-escaping, the source file that protects the HTML tag is displayed. There are two ways to turn off automatic HTML escaping in Django, and if it's a separate variable, we can tell Django that this code is safe without escaping with the filter "|safe". Like what:

Value= "<href=" "> click </a>  "{{Value|safe}}

5. Label of the template

The label looks like this: {% tag %} . Tags are more complex than variables: Some create text in the output, some control the flow through loops or logic, and some of the additional information that is added to the template is used by the variables that are loaded later.

Some tags need to start and end tags (such as label content ...). {% tag %} ... {% Endtag%}).

For label

Iterate through each of the elements

{% for who in person_list%}     < P > {{Person.name}} </ P > {% endfor%}

You can use the {% for obj in list reversed %} reverse to complete the loop.

Traverse a dictionary

{% for key,val in dic.items%}     < P > {{Key}}:{{val}}</p>{% endfor%}

Template layer of Python--django

Related Article

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.