這篇文章帶給大家的內容是關於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 id="nbsp-nbsp-kl-nbsp"> {{ kl }}</h1> <h2 id="nbsp-nbsp-a-nbsp"> {{ a }}</h2> <h3 id="nbsp-nbsp-b-nbsp"> {{ b }}</h3> <h4 id="nbsp-nbsp-c-nbsp"> {{ c }}</h4> </body> </html>
在urls.py 中記得新增路徑
url(r"index", views.index),
效果:
以上是Django使用locals() 函數的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了版本3.10中介紹的Python的新“匹配”語句,該語句與其他語言相同。它增強了代碼的可讀性,並為傳統的if-elif-el提供了性能優勢

Python中的功能註釋將元數據添加到函數中,以進行類型檢查,文檔和IDE支持。它們增強了代碼的可讀性,維護,並且在API開發,數據科學和圖書館創建中至關重要。

本文討論了Python中的單位測試,其好處以及如何有效編寫它們。它突出顯示了諸如UNITSEST和PYTEST之類的工具進行測試。

文章討論了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化對象屬性中的作用。還涵蓋了其他類方法和繼承對\ _ \ _ Init \ _ \ _()的影響。

本文討論了python中@classmethod,@staticmethod和實例方法之間的差異,詳細介紹了它們的屬性,用例和好處。它說明瞭如何根據所需功能選擇正確的方法類型和DA

Inpython,YouAppendElementStoAlistusingTheAppend()方法。 1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具