1. 程式人生 > >《Django》中模板接收到的幾種資料型別

《Django》中模板接收到的幾種資料型別

第一種:接收普通的基本資料型別

接收基本資料型別,views.py中通過賦值的方式傳送給模板檔案,例子如下:

模板檔案index.html程式碼如下

<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
    </head>
    <body>
        <h1>hello ,world</h1> 
        <h1>hello,{{name}}</h1
>
<!--接收基本資料型別--> </body> </html>

views.py程式碼如下

from django.http import HttpResponse
from django.template import loader,Context
def index(request):
    t=loader.get_template("index.html")#載入模板檔案
    #這裡的"title"和"name"對應著index.html檔案中的title和name,
    c=Context({"title":"firstPage"
,"name":"wuranghao"})#通過賦值的方式傳送給模板檔案 return HttpResponse(t.render(c))

第二種:模板檔案接受字典資料型別,可以通過key得到value

當模板檔案接受到字典資料型別時,可以通過字典資料型別的key來訪問value。
例子如下:
其中index.html檔案的內容如下:

<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
    </head
>
<body> <h1>hello ,world</h1> <h1>hello,
{{name}}</h1> <h1>user:{{user}}</h1><!--接收字典資料型別,如果直接將字典型別輸出,則就是將字典資料型別作為字串直接輸出--> </body> </html>

views.py中程式碼如下:即user為字典型別

from django.http import HttpResponse
from django.template import loader,Context
def index(request):
    t=loader.get_template("index.html")
    user={"name":"wuranghao","age":22,"sex":"male"}
    c=Context({"title":"secondPage","name":"wuranghao","user":user})#user就是作為字典型別傳入給模板
    # c=Context({"title":"firstPage","name":"wuranghao"})
    return HttpResponse(t.render(c))

執行結果如下:

從結果可以看出,直接將字典型別以字串的形式輸出了。

如果只想使用字典資料型別的某個值,模板檔案可以通過字典的key來訪問value。則直接修改模板檔案index.html檔案。
例如:訪問字典資料型別中各個key得到value。

<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
    </head>
    <body>
        <h1>hello ,world</h1> <!--直接輸出-->
        <h1>hello,{{name}}</h1>
        <h1>user:{{user}}</h1>
        <li>user:{{user.name}}</li>
        <li>user:{{user.age}}</li>
        <li>user:{{user.male}}</li>
    </body>
</html>

執行結果如下:

第三種:模板檔案可以接收物件資料型別,通過訪問物件的屬性名和方法名來得到內容

index.html程式碼如下:

<html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
    </head>
    <body>
        <h1>user:{{user}}</h1>
        <li>user:{{user.name}}</li><!--訪問屬性-->
        <li>user:{{user.age}}</li>
        <li>user:{{user.sex}}</li>
        <li>{{user.getName}}</li> <!--訪問user的方法,只有方法名,沒有後面的括號-->
    </body>
</html>

views.py中的程式碼如下:

# Create your views here.
from django.http import HttpResponse
from django.template import loader,Context
class Person(object):#定義了一個類
    def __init__(self,name,age,sex):#構造方法
        self.name=name
        self.age=age
        self.sex=sex
    def getName(self):
        return "my name is  "+self.name
def index(request):
    t=loader.get_template("index.html")
    person=Person("wu",19,"female")
    c=Context({"title":"thirdPage","user":person}) #將物件person傳入給模板
    return HttpResponse(t.render(c))

執行程式碼如下:

第四種:模板接收list資料型別,通過索引訪問

例子如下:
views.py中程式碼如下:

# Create your views here.
from django.http import HttpResponse
from django.template import loader,Context
class Person(object):
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
    def getName(self):
        return "my name is  "+self.name
def index(request):
    t=loader.get_template("index.html")
    book_list=["java","python","c"]
    c=Context({"book_list":book_list})#傳入list
    return HttpResponse(t.render(c))

模板檔案index.html通過索引來訪問

<html lang="en">
    <head>
    <meta charset="UTF-8">
    <!-- <title>{{title}}</title> -->
    </head>
    <body>
        <h1>book_list:{{book_list}}</h1>
        <li>{{book_list.0}}</li>
        <li>{{book_list.1}}</li>
        <li>{{book_list.2}}</li>

    </body>
</html>