PHP前端开发

Django使用locals() 函数的方法介绍

百变鹏仔 2周前 (01-23) #Python
文章标签 函数

本篇文章给大家带来的内容是关于Django使用locals() 函数的方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

locals() 函数会以字典类型返回当前位置的全部局部变量。

在 views.py 中添加

from django.shortcuts import render,HttpResponse,render_to_responseimport datetimefrom blog import modelsdef 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

nbsp;html&gt;    <meta>    <title>Title</title><h1> {{ kl }}</h1><h2> {{ a }}</h2><h3> {{ b }}</h3><h4> {{ c }}</h4>

在 urls.py 中 记得添加路径

url(r"index", views.index),

效果: