Python - Django - the template language variables

Preface:

In Django template language variables {} {}% {%} logic with

Add in the correspondence between the urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^test/', views.django_test),
]

Ordinary variables:

Write django_test function in views.py

Import the render django.shortcuts from, redirect, HttpResponse 
from app01 Import Models 


# Django template language variables 
DEF django_test (Request): 
    name = "John" 
    Age = 28 
    return the render (Request, "test.html", { "NAME1": name , "age1": age}) # html pass through a dictionary

Then write test.html page

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
    <Meta charset = "UTF-8"> 
    <title> Django template language test </ title> 
</ head> 
<body> 

<h1> Django template language test </ h1 of> 

{{{{AGE1 NAME1}}}} 

</ body> 
</ HTML>

running result:

If you are using a variable name that does not exist, will not be displayed on the page

List:

If you want to display a list of the members then need to use a loop to display

views.py:

from django.shortcuts import render, redirect, HttpResponse
from app01 import models


# Django 模板语言变量
def django_test(request):
    name = "John"
    age = 28
    hobby = ["Reading", "Basketball", "Movie", "Music"]
    return render(request, "test.html", {"name1": name, "age1": age, "hobby_list": hobby})

test.html:

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
    <Meta charset = "UTF-8"> 
    <title> Django template language test </ title> 
</ head> 
<body> 

<h1> Django template language test </ h1 of> 

<-! Common variable -> 
{{{{AGE1 NAME1}}}} 

<-! listing -> 
{%}% for Hobby in hobby_list 
    {Hobby} {} 
{%} endfor% 

</ body> 
</ HTML>

running result:

dictionary:

{{Value acquired by the dictionary name .key}}

views.py:

from django.shortcuts import render, redirect, HttpResponse
from app01 import models


# Django 模板语言变量
def django_test(request):
    name = "John"
    age = 28
    hobby = ["Reading", "Basketball", "Movie", "Music"]
    info = {"height": 188, "weight": 120}
    return render(request, "test.html", {"name1": name, "age1": age, "hobby_list": hobby, "info": info})

test.html:

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
    <Meta charset = "UTF-8"> 
    <title> Django template language test </ title> 
</ head> 
<body> 

<h1> Django template language test </ h1 of> 

<-! Common variable -> 
{{{{AGE1 NAME1}}}} 

<-! listing -> 
{%}% for Hobby in hobby_list 
    {Hobby} {} 
{%} endfor% 

<! - Dictionary -> 
{{info.height}} | {} {} info.weight 

</ body> 
</ HTML>

running result:

class:

{{Class by object name attribute access properties}}, {{objects by name.}} Method to implement the method

views.py:

from django.shortcuts import render, redirect, HttpResponse
from app01 import models


# 测试类 Animal
class Animal(object):
    def __init__(self, specie, name):
        self.specie = specie
        self.name = name

    def sleep(self):
        return "%s is sleeping" %self.name

# Django 模板语言变量
def django_test(request):
    name = "John"
    age = 28
    hobby = ["Reading", "Basketball", "Movie", "Music"]
    info = {"height": 188, "weight": 120}
    a1 = Animal("Cat", "Tom")
    a2 = Animal("Dog", "Jim")
    return render(
        request,
        "test.html",
        {
            "name1": name,
            "age1": age,
            "hobby_list": hobby,
            "info": info,
            "a1": a1,
            "a2": a2,
        })

test.html:

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
    <Meta charset = "UTF-8"> 
    <title> Django template language test </ title> 
</ head> 
<body> 

<h1> Django template language test </ h1 of> 

<-! Common variable -> 
{{{{AGE1 NAME1}}}} 

<-! listing -> 
{%}% for Hobby in hobby_list 
    {Hobby} {} 
{%} endfor% 

<-! Dictionary -> 
{{info.height}} |}} {{info.weight 

<br> 

<- class -!> 
{{a1.specie}} |}} {{a1.name | a1.sleep} {} { 
<br> 
{} {} a2.specie | a2.name {} {} | {} {} a2.sleep 

</ body> 
</ HTML>

running result:

 

Guess you like

Origin www.cnblogs.com/sch01ar/p/11247553.html