django template template

Nine .Template template 
Template function html template is displayed, the typical template jinjia2 python library provides a rich context based on display func pass over view data 
created template position in the project with the same level under the app directory 
configuration template path 9.1: 
modifying settings.py , bASE_DIR project directory is the dir: 
TEMPLATES = [ 
    { 
        'BACKEND': 'django.template.backends.django.DjangoTemplates', 
        'the DIRS': [the os.path.join (base_dir, 'Templates')], 
        'APP_DIRS' : True, 
        'the OPTIONS': { 
            'context_processors': [ 
                ' django.template.context_processors.debug ', 
                ' django.template.context_processors.request ', 
                ' django.contrib.auth.context_processors.auth ', 
                ' Django.contrib.messages.context_processors.messages',
            ], 
        },
    },
]

9.2templates Syntax: 
{} {} represents the variable varname 
{%} for var% in Object 
<P> var.age {{}} </ P> 
{%} endfor% 
# Syntax: 
{{Variable | filter} } 
# example: 
# automatically escapes HTML 
{{name | Safe}} 
{{name | Lower}} 
# represents the value of the variable name in all lowercase 
# vertical bar | python may be understood as the dot (.) 
# if the label can be a filter is used, in conjunction with the operator using 
{% if user_list%} 
  number of users: {{user_list | length}} 
{%}% elif black_list 
  number blacklist: {{black_list | length}} 
{ % else%} 
  no user 
{% endif%} 
# other 
{IF name% | length> 2%} 
# filters can be used in series, constituting the filter chain 
name | Lower | Upper 
# filter may pass parameters 
list | join: ""
# Default filter setting 
value | default: "0" 
# Date Set
value | DATE: "mm-dd-YYYY" 

#templates ORG Help 
# https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/ 

Template Custom Tag: 
from Django Import Template 
Register Template = .Library () # generates an instance object registered 
@ register.simple_tag () 
DEF SE (arg1, arg2): 
    return arg1 arg2 + 
# HTML 
<H3> simple_tag exemplary self-defined </ H3> 
    {%}% Load S1 
    < p> {% se 2 4% } </ p>

  

Guess you like

Origin www.cnblogs.com/SunshineKimi/p/11757575.html