locals()函数会以字典类型返回当前位置所有的局部变量。
在Django视图中可以这样使用:
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())
就会返回字典{‘kl’:‘you are welcome’,‘a’:‘hello’,‘b’:‘world’,‘c’,‘what’}