locals() 函数会以字典类型返回当前位置的全部局部变量。
在 views.py 中添加
from django.shortcuts import render,HttpResponse,render_to_response
import datetime
from blog import models
def index(req):
if req.method=="POST":
username = req.POST.get("username")
pwd = req.POST.get("password")
print(username)
print(pwd)
if username == "klvchen" and pwd=="123":
return HttpResponse("登录成功")
#return render(req, "login.html")
kl = "you are welcome"
a = "hello"
b = "world"
c = "what"
return render_to_response("new.html", locals())
在 templates 添加 new.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> {{ kl }}</h1>
<h2> {{ a }}</h2>
<h3> {{ b }}</h3>
<h4> {{ c }}</h4>
</body>
</html>
在 urls.py 中 记得添加路径
url(r"index", views.index),
效果: