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

    效果:

  • 相关阅读:
    快速幂,矩阵乘法,矩阵快速幂
    关于xor
    数位dp
    tarjan,割边,桥,割点
    RMQ,ST表,dp
    逆序对,树状数组,归并排序
    线段树
    dp,LCS
    清北 游
    青蛙的约会(扩展欧几里得)
  • 原文地址:https://www.cnblogs.com/klvchen/p/10697179.html
Copyright © 2011-2022 走看看