在django中创建html模板,关联数据,返回给浏览器
修改settings.py文件
将 'DIRS': [] 修改为'DIRS': [os.path.join(BASE_DIR, 'templates')],
项目目录下创建模板文件夹templates
templates下创建模板文件index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body class="globcss">
<table border="0" class="tablecss"> <tr> <td class="keycss">{{key0}}</td> <td class="valuecss"> {{value0}}</td> </tr> </table>
<div class="line"></div>
<table border="0" class="tablecss"> <tr> <td class="keycss">{{key10}}</td> <td class="valuecss"> {{value10}}</td> </tr> </table>
<div class="line"></div>
</body>
</html>
<style>
.globcss {
font-size: 36px;
line-height: 2em;
width: 100%;
height: 100%;
background-color: #f3f4f6;
}
.tablecss {
width: 100%;
}
.keycss {
color: green;
width: 18%;
text-align-last: justify;
}
.valuecss {
color: #000000;
}
.trcss {
margin-top: 100px;
}
.div1{
}
.line{
width: 100%;
border-bottom: 1px solid #000000;
color:green;
}
.test{
background-color: #a3f4f6;
}
</style>
views.py编写
from django.shortcuts import render
from django.http import HttpResponse
from django.core import serializers
def get_data(request):
...
return render(request, "index.html",{"key0": "11", "value0": "22"})
#return render(request, "err.html", {"err": "404"})
urls.py指定路由
...
浏览器访问测试
#html文件有修改,保存并刷新浏览器即可显示最新内容。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/287801.html