Django (b) template

First, the concept of templates

1.Django dynamically generated html template

2. loading position template

Templates are generally built on templates folder, set the global path in the settings.py

DIRS: determines the location of the template path of the entire project

APP_DIRS: decision template path is available for each application, whether to find the template in the templates directory of the application

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        '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',
            ],
        },
    },
]

3. Template engine

The framework uses Django Django template engine, essentially a class that implements the relevant functions, inherited from BaseEngine

You can explain template variables and template tags

Second, the template variables

1. Grammar

html direct access variables: variable name} {} {

Dot syntax to access the type of complex variables, such as variables list, dict, obj type

Only use Access does not support negative

from django.shortcuts import render


def pass_dict(request):
data = {
'name':'apple',
'price':12.5,
'color':'yellow'
}
return render(request,'var/var_demo.html',{'mydict':data})


Cake class: 
DEF __init __ (Self, name,. price):
self.name = name
self.price. price =

DEF order_cake (Self):
return 'to order a name:' + str (self.name) + ' from:' + str (self.price) + 'cake'


DEF pass_object (Request):
cake = the cake ( 'birthday cake', 12.4)
return the render (Request, 'var / var_object.html', { 'cake': cake})


pass_list DEF (Request):
Fruits = [ 'Apple', 'dragon', 'banana', 'pineapple']
return the render (Request 'var / var_list.html' { 'Fruits': Fruits})
<! DOCTYPE HTML > 
< HTML lang = "EN" > 
< head > 
    < Meta charset = "UTF-. 8" > 
    < title > the Title </ title > 
</ head > 
< body > 
    Fruit Name: {{mydict.name} } < br > 
    fruit price: mydict.price {{}} < br > 
    fruit color: mydict.color {{}} < br > 
</ body > 
</ HTML >

<! DOCTYPE HTML > 
< HTML lang = "EN" > 
< head > 
    < Meta charset = "UTF-. 8" > 
    < title > the Title </ title > 
</ head > 
< body > 
    Cake Name: {{cake.name} } 
    cake price: {{cake.price}} 
    calling cake no parameters (except the self argument) method: cake.order_cake {{}} 
</ body > 
</ HTML >
<! DOCTYPE HTML > 
< HTML lang = "EN" > 
< head > 
    < Meta charset = "UTF-. 8" > 
    < title > Fruits </ title > 
</ head > 
< body > 
    list of the first item: {{fruits }} .0 
    list third: fruits.2 {{}} 
</ body > 
</ HTML >

 

Guess you like

Origin www.cnblogs.com/huiyichanmian/p/11234720.html