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


本文摘自php中文网,作者不言,侵删。

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

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

在 views.py 中添加

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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

1

2

3

4

5

6

7

8

9

10

11

12

13

<!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 中 记得添加路径

1

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

效果:

以上就是Django使用locals() 函数的方法介绍的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python中的栈指的是什么

利用Python将图片转换成excel文档格式详解

对numpy中array和asarray的区别

Python中函数参数的详细介绍(附实例)

如何用Python正则表达式匹配字符串?

Python绘制正方形螺旋线

pytorch快速搭建神经网络及其保存提取方法详解

Python语言支持编程方式有哪些?

Python这68个内置函数,建议你吃透!

Python怎么看数据类型

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...