上一篇,我们学习了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.time
return 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 views
urlpatterns = [
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.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
import time
def index(request):
return HttpResponse("hello")
def demo(request):
create_time = time.time
return 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 time
def 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_li
st': skill_list})
访问http://127.0.0.1:8000/demo