views.py中的代码:
from django.shortcuts import render from django.views import View class IndexViews(View): def get(self, request): datas = [ { "projects_name": "胶片", "leader": "奔奔1", "projects_app": "公众号" }, { "projects_name": "组学", "leader": "奔奔2", "projects_app": "公有云" }, { "projects_name": "AI-BOX", "leader": "奔奔3", "projects_app": "私有云" }, ] # return HttpResponse("<h1>Hello, Django</h1>") return render(request, 'projects.html', locals())
projects.html中的模板代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>项目列表</title> </head> <body> <table> <tr> <td>ID</td> <td>项目名称</td> <td>项目负责人</td> <td>应用名称</td> </tr> {% for project in datas %} <tr> <td>{{ forloop.counter }}</td> <td>{{ project.projects_name }}</td> <td>{{ project.leader }}</td> <td>{{ project.projects_app }}</td> </tr> {% endfor %} </table> </body> </html>
运行时如果报错:TemplateDoesNotExist at
在全局setting.py文件中TEMPLATES的值修为为:
'DIRS': [os.path.join(BASE_DIR, 'templates')],