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文件代碼
案例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模板变量传参的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JMeter】Threads(user
- 下一篇: Pytest之pytest.assume