Python Day 63 Django framework (the role of rendering of the page) Django template system

  ## Common Grammar

Only need to remember two special symbols: 
{} {} and { %% } 
variable associated with {{}}, {logic associated with %%}.

  ## Data type rendering

#1、后端视图函数代码
def template_test(request):
    l = [11, 22, 33]
    d = {"name": "alex"}

    class Person(object):
        def __init__(self, name, age):
            self.name = name
            self.age = age

        def dream(self):
            return "{} is dream...".format(self.name)

    Alex = Person(name="Alex", age=34)
    Egon = the Person (name = " Egon " , Age = 9000 ) 
    Eva_J = the Person (name = " Eva_J " , Age = 18 is ) 

    person_list = [Alex, Egon, Eva_J]
     return the render (Request, " template_test.html " , { " l " : l, " D " : D, " person_list " : person_list}) 

# 2, a front end supported in the wording template 
{ # take the first parameter l} # 
{l.0} {} 
{ # take Dictionary the # key value}
D.name}} {{ 
{ # take the object name attribute} # 
{person_list.0.name} {} 
{ # . Calls not only operating parameters of the method} # 
{} {} person_list.0.dream

  ## Filter (filter)

In Django template language by using filters to change the display of variables. 
The syntax of the filter: {{value | FILTER_NAME:}} parameter 
using the pipe symbol " | " applying a filter. 
For example: {{name | lower}} then the variable name will show its value after the application of lower filter. lower in action here is all lowercase text. 


# Note: 
1 , filter supports the "chain" operation. I.e., a filter output as input to another filter.
2, the filter can accept parameters, for example: {{sss | truncatewords: 30 }}, which will display the first 30 words of sss.
3, the filter parameter includes a space, it must be wrapped in quotes. For example, use a comma and a space to connect the elements of a list, such as: {{List | the Join: ' , ' }}
 4, ' | ' about no space is no space without a space 

# Django template language provides approximately sixty built-in filter. 
1 , default 
if a variable is false or empty, the default values given. Otherwise, use the value of the variable.
Value {{ | default: " Nothing " }}

 2 , length 
character string and list length, the role of the return value. Value {{ | length}}

 . 3 , filesizeforma 
value formatted as a "human readable" file size (e.g. ' 13 is KB ' , ' 4.1 MB ' , ' 102 bytes ' , etc.). For example: If the value is 123456789, the output will be 117.7 MB. 
Value {{ | filesizeformat}}

 . 4 , Slice 
Slice 
{{value | Slice: " 2: -1 " }}
 . 5 , DATE 
format: {{value | DATE: " Ymd H: I: S " }}

 . 6, Safe (we call Xss cross-site scripting attacks) 
template in Django will HTML tags and labels for automatic syntax such as JS escaped, for obvious reasons, this is for safety. But sometimes we might not want these HTML elements are escaped, for example, we do a content management system, add background of the article is modified, these modifications may be raised by a similar FCKeditor editing the HTML modifier text, escaped, then automatically displayed if the source file is to protect HTML tags. To turn off the automatic Django escaped HTML in two ways, if it is a single variable that we can pass through the filter " | tell Django safe" way to secure this code is not necessary to escape. 
Value {{ | Safe}}

 . 7 , truncatechars 
if the character string is more than a specified number of characters, it is truncated. Translatable strings will be truncated sequence at the end of the ellipsis ( "..."). 

Parameters: number of characters truncated 
{{value | truncatechars:. 9 }}

 . 8 , truncatewords 
truncates the string after a certain number of words. 
Value {{ | truncatewords:. 9 }}

 . 9 , Cut 
remove all the given value of the same variable character string 
{{value }}| Cut: '  '
If value is ' I Love you ' , then the output will be ' ILOVEYOU '
 
10 , the Join 
string connection list, for example, the Python str.join (list)

   Custom filter ## Filter

# Create a fixed directory name must be templatetags 
    1 , create a directory app in templatetags
     2 , create a custom filter files in the directory 

# to write custom filter (there are two ways, one can only pass a parameter and another plurality of parameters can be passed, not the same two ways to use the front end, but the embodiment is the same as the introduction)
 
- simple_filter: 
    
    from Django import Template 

    Register = template.Library () 

    @ register.filter () 
    DEF my_func (Val, arg1):
         return Val + arg1 
    
    Note:
         1 can only pass a parameter
         2 with no spaces between the function name and parameters 
    front end usage: 
                { # first import our custom filter that file} # 
        {the Load% xx%}   
                 { # Our #} custom filter 
        {{name | my_func: ' kkkk ' }}

 - the simple_tag: 

    from Django Import Template 

    Register = template.Library () 
    
    @ register.simple_tag () 
    DEF my_func (Val, arg1):
         return Val + arg1 
    
    usage: 
        { % Load XX% } 
        { % my_tag ' Zekai '  ' IS '  ' JJJ ' % }
    

   ## for circulation

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

  ## if judgment

% { IF user_list% } 
  the number of users: {{user_list | length}} 
{ % elif black_list% } 
  blacklist number: {{black_list | length}} 
{ % the else % } 
  There are no user 
{ % endif% } 


{ % IF user_list | length>. 5% } 
  seven luxury the SUV 
{ % the else % } 
    rickshas 
{ % endif% } 

IF statement supports and , or , ==,>, <,! =, <=,> =, in , Not inisis not判断。

  ##with

Define an intermediate variable, to give a complex multi-variable aliases. 
Note that no space around the equal sign. 
{ % With business.employees.count Total =% } 
    {} {} Total Total Employee {{ | the pluralize}} 
{ % endwith The% } 

or 
{ % Total% AS with business.employees.count } 
    {} {} Total Employee { Total { | the pluralize}} 
{ %}% endwith The

  Master ## and daughterboard

# Note: We usually define the page dedicated block in the motherboard CSS and JS block to facilitate sub-page replacement. 
<! DOCTYPE HTML> 
<HTML lang = " EN " > 
<head> 
  <Meta charset = " UTF-. 8 " > 
  <Meta HTTP-equiv = " X-UA-compatible " Content = " IEs = Edge " > 
  <Meta name = " the viewport " Content = " width = width-Device, Initial-Scale =. 1 " > 
  <title> the title </ title>
  

<h1> It is motherboard title </ h1 of> 

{ % Block Page main-% } 

{ % endblock% }
 <h1> Motherboard bottom SUMMARY </ h1 of> 
{ % Block Page JS-% } 

{ % endblock% }
 </ body> 
</ HTML> # daughterboard inherit the master in the sub-page using the following syntax at the top of the page to inherit the motherboard. 
{ % The extends ' layouts.html ' % } # in the corresponding sub-page to replace the mother board by defining the contents of the corresponding block in the motherboard name 
{% block Page main-% }
   <P> Thin world situation </ P> 
  < p> human evil </ p> 
  <p> rain fall easily spend the evening feeding </ p> 
{ %}% endblock




  ## components

As conventional navigation page content, footer information components may be stored in a separate file, and then introduced into the following syntax can be used where necessary. 

{ % The include ' navbar.html ' %}

  ## static files related

# . 1, static%} {% 
{static% Load% }
 <IMG the src = " {% static " Images / hi.jpg " %} " Alt = " ! The Hi " /> # use 2, reference JS file: 
{ % Load static% }
 <Script the src = " {% static " mytest.js " %} " > </ Script> # . 3, a multiple document is saved as a variable may be used 
{ % Load static% } 
{ % static " Images / hi.jpg " AS MyPhoto% }
 <IMG the src = "MyPhoto {{}}




"></img>

#4、{% get_static_prefix %}
{% load static %}
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!" />

 

Guess you like

Origin www.cnblogs.com/liangzhenghong/p/11209664.html