当前位置: 移动技术网 > IT编程>脚本编程>Python > Django 使用 locals() 函数

Django 使用 locals() 函数

2019年04月13日  | 移动技术网IT编程  | 我要评论

苏樱姐,谢紫彬,语文教学案例

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),

效果:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网