日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python测试开发django5.templates模板变量传参

發布時間:2025/3/15 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python测试开发django5.templates模板变量传参 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇,我們學習了Python測試開發django4.templates模板配置

? ? ? templates模板中html文件是一個靜態頁面,寫四的,如果有時我們想動態的傳入一些不同的參數,想實現在一個固定的html樣式,這就可以用django的模板變量傳參來解決。

項目目錄

模板語法

helloworld\hello\templates\demo.html 文件中用{{html變量名}}表示變量名

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>demo模板</title> </head> <body><p><h4> 我的博客 </h4><a href="https://blog.csdn.net/qq_36502272" target="_blank" > {{name}} </a><hr><h4> 軟件測試技術交流分享-創建時間{{create_time}} </h4><p>軟件測試技術、方法、測試方案分享交流、Python自動化測試交流學習、性能Jmeter工具交流學習<br>QQ交流群212683165</p><a href="https://blog.csdn.net/qq_36502272" target="_blank" >點擊訪問博客</a> </p></body> </html>

helloworld\hello\views.py?文件中用{'html變量名':傳views變量名或'傳值'}表示傳參

#?傳views變量名 def demo(request):name_dict = {'name': '橙子探索測試'}skill_list = ['python自動化測試','測試方案','jmeter性能自動化測試']return render(request, 'demo.html', {'name_dict': name_dict, 'skill_list': skill_list})# 傳值? def demo(request):create_time = time.timereturn render(request, 'demo.html', {'create_time': create_time, 'name': '橙子探索測試'})

模板路徑

向django說明模板的路徑,helloworld\helloworld\settings.py文件里配置模板路徑'DIRS'為?[BASE_DIR+"/templates",],

TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [BASE_DIR+"/templates",],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},}, ]

helloworld\helloworld\urls.py文件代碼

from django.contrib import admin from django.urls import path from django.conf.urls import url from hello import viewsurlpatterns = [path('admin/', admin.site.urls),url('^demo$', views.demo), ]

案例1

helloworld\hello\templates\demo.html設置變量{{name}}、{{create_time}}

helloworld\hello\views.py?設置傳參{'create_time': create_time, 'name': '橙子探索測試'}

傳參字典中的key create_time、name對應模板中的變量{{create_time}}、{{name}},根據key取出對應的value

# demo.html<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>demo模板</title> </head> <body><p><h4> 我的博客 </h4><a href="https://blog.csdn.net/qq_36502272" target="_blank" > {{name}} </a><hr><h4> 軟件測試技術交流分享-創建時間{{create_time}} </h4><p>軟件測試技術、方法、測試方案分享交流、Python自動化測試交流學習、性能Jmeter工具交流學習<br>QQ交流群212683165</p><a href="https://blog.csdn.net/qq_36502272" target="_blank" >點擊訪問博客</a> </p></body> </html> # urls.pyfrom django.shortcuts import render from django.http import HttpResponse # Create your views here. import timedef index(request):return HttpResponse("hello")def demo(request):create_time = time.timereturn render(request, 'demo.html', {'create_time': create_time, 'name': '橙子探索測試'})

訪問http://127.0.0.1:8000/demo

案例2

helloworld\hello\templates\demo.html設置變量{{name_dict}}取整個字典、{{name_dict.name}}取字典中的name值,{{skill_list}}取整個列表、{{skill_list.0}}取列表中第1個值

helloworld\hello\views.py?設置傳參{'name_dict': name_dict, 'skill_list': skill_list}

# demo.html<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>demo模板</title> </head> <body><p><h4> {{name_dict}},{{skill_list}} </h4><a href="https://blog.csdn.net/qq_36502272" target="_blank" > {{name_dict.name}} </a><hr><h4> 軟件測試技術交流分享{{create_time}} </h4><p>軟件測試技術、方法、測試方案分享交流、{{skill_list.0}}、性能Jmeter工具交流學習<br>QQ交流群212683165</p><a href="https://blog.csdn.net/qq_36502272" target="_blank" >點擊訪問博客</a> </p></body> </html> # urls.py from django.shortcuts import render from django.http import HttpResponse # Create your views here. import timedef index(request):return HttpResponse("hello")def demo(request):name_dict = {'name': '橙子探索測試'}skill_list = ['python自動化測試','測試方案','jmeter性能自動化測試']return render(request, 'demo.html', {'name_dict': name_dict, 'skill_list': skill_list})

訪問http://127.0.0.1:8000/demo

【UI設計、平面設計、LOGO設計需求】商務合作QQ:2017340535

【軟件測試方案設計、測試方法指導】商務合作QQ:2017340535

總結

以上是生活随笔為你收集整理的Python测试开发django5.templates模板变量传参的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。