1、index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>django之创建第4-2个项目</title>
</head>
<body>
    <h2>访问list数据,用索引获取list内的数:{{test.0}}</h2> <!--模板  变量用变量定义-->
    <h2>访问list数据,用索引获取list内的数:{{test.3}}</h2>
    <h2>访问类方法:{{test.myMethod}}</h2>
</body>
</html>

 

2、views.py

# Create your views here.
#coding:utf-8
from django.http import HttpResponse

#导入templates文件所需导入库
from django.template import loader,Context


class Person():

    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex

    def myMethod(self):
        return "get it"

def index(request):
    #第二个项目
    #return HttpResponse("hello,Django")

    #加载器,加载模板
    t=loader.get_template("index.html")

    #django之创建第4-3个项目-访问list数据
    book_list = ["python","c++","ruby","php"]
    c = Context({"test": book_list})  # 在这里test位变量,book_list为变量的值
    return HttpResponse(t.render(c))

3、百度云盘:http://pan.baidu.com/s/1nuTzELr

相关文章:

  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-11-23
  • 2022-01-18
相关资源
相似解决方案