We views.py
see that often render function. It is how to render it into html?
def home(request):
return render(request,'home.html')
In fact, it implements two steps:
In views.py
add
def home2(request):
template = loader.get_template('home2.html') # 1.先加载
words = {
"word":"Hello!!Home"
}
resuel = template.render(words) # 2.将数据填写到模板中
print(resuel)
return HttpResponse(resuel)
In urls.py
adding:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>{{ word }}</h1> # 填写的地方
</body>
</html>
Start the project, and access
Back-end output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Hello!!Home</h1> # 填写项
</body>
</html>