Django- template rendering process

We views.pysee 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.pyadd

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.pyadding:

<!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

Here Insert Picture Description

Back-end output:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<h1>Hello!!Home</h1>			# 填写项
</body>
</html>

Published 58 original articles · won praise 4 · Views 1939

Guess you like

Origin blog.csdn.net/weixin_43999327/article/details/104071642