• 技术文章 >后端开发 >Python教程

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

    不言不言2019-04-15 11:03:11转载1687

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

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

    效果:

    以上就是Django使用locals() 函数的方法介绍的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除
    专题推荐:Django
    上一篇:Python循环的技巧介绍(附代码) 下一篇:python函数参数默认值的用法及注意要点
    Web大前端开发直播班

    相关文章推荐

    • Django中ModelForm组件的介绍(代码示例)• Django自定义模板标签和过滤器(代码示例)• 编写一个简单的 Django 应用• django源码分析之请求流程• pycharm中Django的安装教程(图文)

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网