Road Django learning 05

Django template layer

Template layer grammar (categories)

Related variables: {} {}

Related logic: {} {}

Template method to pass values

# 1 
# a dictionary traditional values, by name, for example, 
return the render (Request, ' reg.html ' , { ' n- ' : n-, ' F ' : F}) 

# Method 2 
return the render (Request, ' REG .html ' , about locals ())
 # about locals () will it all in the name of all of the namespace passed to the front 
# drawback is that if you do not need so many front-end data will result in a waste of resources

Related variables

After testing, the Python integer, float, strings, lists, dictionaries, and a set of tuples are passed to a front end

# Template supported written 
{ # take the first parameter l} # 
{l.0} {} 
{ # take the dictionary key value} # 
{d.name} {} 
{ # take the object name attribute # } 
{} {} person_list.0.name 
{ # . # operation method can only be called with no arguments} 
{} {} person_list.0.dream

Added: you can also pass the function name, parentheses are automatically invokes this function, the return value after the front-end will display a function call, if the function needs to pass the Senate, then no, because the template syntax is not supported.

The filter template syntax

In Django template language by using filters to change the display of variables.

The syntax of the filter: {{value | filter_name: Parameter}}

Use pipe symbol "|" applying a filter.

# Length 
{ # <P> {{L | length length}} return value </ P> #} 

# default 
{ # <P> {{SS | default: 'When | left variable will return null | the right value '}} default get method with a rear end you like </ P> #} 
{ # <P> {{SS | default:' '}} default must have two parameters </ P> #} 

# filesizeformat 
{ # <P> {{FILE_SIZE |}} filesizeformat value converted to a file size </ P> #} 

# trunchatewords 
{ # <P> {{info | truncatewords: press space. 3}} is not taken three points </ P> #} 

# truncatechars 
{ # <P> {{info | truncatechars:}}. 6 by character content taken three points can be considered </ P> #} 

# Dare 
{ # <P>{{Ctime | date: 'Ymd '}} only need to have the date on it </ the p->} # 

#the Add 
{ # <P> {{n-| the Add: 100}} </ P> #} 
{ # <P> {{S | the Add: 'hahah roll friends'}} plus </ P> #} 

# Slice 
{ # <p> {{value | slice : "2: 1"}} slice, care regardless of the end to be a predetermined step size </ p> #}
# Safe 
{ # <the p-> {{xxx | Safe}} </ the p-> #} 
{ # <the p-> {{yyy | Safe}} </ the p-> #} 
"" " 
template in Django will HTML tags and JS and other grammatical label automatically escaped, for obvious reasons, this is for security, but sometimes we may not want these HTML elements are escaped, in order to turn off automatic HTML escaping in Django, we can pass through the filter. "| safe "way to tell Django code does not have to be a safe escape. " 
""

Custom filter

Custom filters with only one or two parameters Python functions:

  • (Inputs) the value of - - is not necessarily a string
  • Values ​​of the parameters - which can have a default value, or omitted entirely,

For example, in the filter {{var | foo: 'bar '}} , the filter foo pass variables var and parameters "bar" .

# Custom concerns's storage location 
app01 / __init__ .py 
    models.py 
    templatetags /   # to create a new package package in app01 below __init__ .py 
        app01_filters.py   # build a custom filter files stored 
    views.py
    
        
#编写自定义过滤器
from django import template
register = template.Library()


@register.filter(name="cut")
def cut(value, arg):
    return value.replace(arg, "")


@register.filter(name="addSB")
def add_sb(value):
    return "{} SB".format(value)
# Front end using custom filters 
{ # to import our custom filter the file #} 
{% Load app01_filters% } 

{ # used our custom filter #} 
{{someVariable | Cut: " 0 " }} 
{{D .name | addSB}}

The label template syntax

for loop

<ul>
{% for user in user_list %}
    <li>{{ user.name }}</li>
{% endfor %}
</ul>

About forloop

if the judge

For foo in% { ''%} 
    {%} IF forloop.first% 
        < P > This is my first </ P > 
        {%}% elif forloop.last 
        < P > This is the last time ah </ P > 
        {%}% the else 
        < P > to be ah ah !!! </ P > 
    {% endif%} 
    {%}% empty 
    < P > when the object for loop is empty will go empty </ P > 
{% endfor%}

with

Define an intermediate variable, to give a complex multi-variable aliases.

Note that no space around the equal sign.

{% with business.employees.count as total %}
    {{ total }} employee{{ total|pluralize }}
{% endwith %}

Custom label

# Custom label 
# support multiple values pass 
@ register.simple_tag (name = ' Jason ' )
 DEF XXX (A, B, C, year):
     return  ' ?% S% S | {S% S% ' % (A , B, C, year) 

# custom inclusion_tag 
"" " 
receiving user then acts on the parameters passed in a html page 
after the rendering data on the page to the rendered page 
into the user places the call inclusion_tag 
" "" 

# from defined inclusion_tag 
@ register.inclusion_tag ( ' bigplus.html ' )
 DEF bigplus (n-): 
    L = []
     for I in Range (n-): 
        L.append(' % S of item ' % I)
     return { ' L ' : L}
#前端
<ul>
    {% for foo in l %}
        <li>{{ foo }}</li>
    {% endfor %}
</ul>

Inheritance and import templates

# First need to be divided into a plurality of modules inherited regions 
{% block from the name given area% } 

{ % endblock% } 

# usually a template should have at least three 
{% block css% } 
page css code blocks 
{ % endblock% } 

{ % block js% } 
page js code block 
{ % endblock% } 

{ %}% block Contet   # attention from the name without quotes 
    page topic content 
{ % endblock%}

Daughter board inherited template

# First template inherits all of the content 
{% the extends ' home.html ' % } 

# is then modified in accordance with block block content name specified area 
{% block Content% }
     <form Action = "" > 
    <P> username: <INPUT type = " text "  class = " form-Control " > </ P> 
    <P> password: <INPUT type = " text "  class = " form-Control " > </ P> 
    </ form> 
{ %}% endblock

Importing templates : import another module as html html showcase some way

The include% { ' want to import the html file name ' %}

 

Guess you like

Origin www.cnblogs.com/wangnanfei/p/11545065.html