(一)定义模板
首先在项目里创建templates文件夹,并在 templates文件下创建company文件名字自己取,尽量跟你的app名字保持一致。
在company下创建html文件
(二)修改settings.py文件,设置TEMPLATES的DIRS值
'DIRS': [os.path.join(BASE_DIR, 'templates')], templates-----创建的模板名
(三)开始对html文件进行编写 其实整个html文件是对界面的一个渲染
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
</head>
<body>
{{departName}}---------------{{}} 固定格式 相当于打印 我们现在把所有的部门打印出来
<ul>--------------------------对部门进行遍历
{{%for name in departName}}
<li>{{departName}}</li>
{{%endfor}}
</ul>
</body>
(四)进入到views.py文件里 template_name:html文件所在的路径。 context 里的东西html可以调用
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
#def haha(request):
# return HttpResponse('haha...')
def departMoban(request):
bookinfo_list =depart.objects.all()
context={ ---------------------这只是个字典,目的是为了存储上面的信息,html文件
'departName':bookinfo_list, -------------------------------------- 里可以调用。
'titlt':'这是部门名'
}
return render(request=request,template_name='company/depart_info.html',context=context)
(五)总结
其实django 遵循 vmt框架
(六)代码测试