zoukankan      html  css  js  c++  java
  • 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),
    

    效果:

  • 相关阅读:
    【BZOJ4300】绝世好题
    codeforce1051F
    【BZOJ3329】Xorequ
    codeforce739c
    【UVA11825】
    codeforce6E
    CF 1023D Array Restoration
    BZOJ 1911 [APIO2010] 特别行动队
    POJ 3709 K-Anonymous Sequence
    BZOJ 2726 [SDOI2012] 任务安排
  • 原文地址:https://www.cnblogs.com/klvchen/p/10697179.html
Copyright © 2011-2022 走看看