Python Great god-Django (deep learning)--template language

Source: Internet
Author: User

Message: Template language: Templates of the creation process, for the template, in fact, is to read the template (which nested template tags), and then the model obtained data into the template, and finally return the information to the user. The template also has its own language, which can implement data presentation one, inheritance (Include,extend) a. Fully consistent

{% include xxx.html%}

B. Inherit the template, design your own special section ({% block block_name%}{% endblock%})

Master: {% block title%}{% Endblock%}

Sub-version: {% extend xxx.html%}
{% block Block_name%} {% Endblock%}

Second, data acquisition
get a single: {{item}} Loop: {for in item_list%}  <a>{{Item}}</a>  {% endfor%} forloop.counter  #  Calculator, starting from 1, forloop.counter

Forloop.first # First Loop forloop.last # last Loop
Third, judgment
{%ifOrdered_warranty%} {%Else%} {% ENDIF%}        {%if  notOrdered_warranty%} {%Else%} {% ENDIF%}                {%ifSort = ='Courses' orSort = ='org' orSort = ='Teacher'% } {%Else%} {% ENDIF%}        {%ifSort = ='Courses'  andSort = ='org'% } {%Else%} {% ENDIF%Note: The OR and and cannot be placed in the same logic {%ifSort = ='Courses'% } {%Else%} {% ENDIF%}        {% ifequal Sort'Courses'%}active{% endifequal%}
Iv. methods provided in the template language
12) StringFormat (conversion of data types)4       #Conversion of data types5{% ifequal City.id|stringformat:'I'city_id%}active2{% endifequal%} 6             73) Split (path split)--Slice8{%ifRequest.path|slice:'7'=='/course'%}{% endif%}9         Ten4) Displays the name of the Choice field (Get_degree_display) One {{Request.user.get_degree_display}} A          -5the meaning of the divisibleby tag is to be removed with the following parameters, except as true, otherwise false.  -{%ifForloop.counter|divisibleby:2%} the          -6) code means: 5/1 *100, return 500,widthratio requires three parameters, it will use the parameter 1/parameter to the parameter 3, so to multiply, then the parameter 2 =1 Django Template Division -{% widthratio 5 1 100%} -          +7) Custom Date display mode -{{item.event_start|date:"y-m-d h:i:s"}} +          A8) at{{bio|truncatewords:" -" }} -          -9) Capitalize first letter -{{my_list|first|Upper}}  -          -10) Variable lowercase in{{Name|lower}}

11) Placing CSRF attacks
{% Csrf_token%} # form form used
Xhr.setrequestheader ("X-csrftoken", "{{Csrf_token}}"); # with post submission must request headband Csrf_token# using the Post method
Five, custom Simple_tag
A, create Templatetags module B in the app, create any. py file, such as: xx.py#!/usr/bin/env python    #Coding:utf-8     fromDjangoImportTemplate fromDjango.utils.safestringImportMark_safe Register=template. Library () @register. Simple_tagdefMy_simple_time (v1,v2,v3):returnV1 + v2 +v3 @register. Simple_tagdefMy_input (id,arg): Result="<input type= ' text ' id= '%s ' class= '%s '/>"%(Id,arg,)returnMark_safe (Result) C, import the xx.py file name created before importing the HTML file that uses the custom Simple_tag {% load xx%}d, using Simple_tag {% My_simple_time 1 2 3%}    {% My_input'Id_username' 'Hide'%}e, configure the current app in Settings, or Django can't find the custom Simple_tag Installed_apps= (        'Django.contrib.admin',        'Django.contrib.auth',        'Django.contrib.contenttypes',        'django.contrib.sessions',        'django.contrib.messages',        'Django.contrib.staticfiles',        'APP01',    )
Custom Methods--Steps

Python Great god-Django (deep learning)--template language

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.