django访问模板列表中的值
问题描述:
我的网页在查看时目前是空白的,看着其他样本,似乎我正确访问值,但我没有得到任何数据。 我已经通过打印,检查了我的名单,他们都具有丰富的数据在其中生成django访问模板列表中的值
模板
{% extends 'oncall/base.html' %}
{% block content %}
{% for pol in lstPolicy %}
<h2>1 - {{ pol.Name }}</h2>
{% for user in lstUsers %}
{% if user.Policy == pol.Name %}
<h3>2 -{{ user.Level }}</h3>
<p>
Mobile: {{ user.Mobile }}
From: {{ user.StartTime }} on {{ user.StartDate }}
Until: {{ user.EndTime }} on {{ user.EndDate }}
</p>
{% endif %}
{% endfor %}
{% endfor %}
{% endblock %}
基本模板
{% load staticfiles %}
<html>
<head>
<title>IT on call Rota</title>
</head>
<body>
<a href="/">Home</a>
{% block content %}
{% endblock %}
</body>
</html>
视图
# Create your views here.
def index(request):
lstPolicy = []
lstUsers = []
for objPolicy in objPolicyData['escalation_policies']:
strPolicyName = objPolicy['name']
if strPolicyName.lower().find('test') == -1:
classPolicy = Policy()
classPolicy.Name = strPolicyName
lstPolicy.append(strPolicyName)
for objOnCall in objPolicy['on_call']:
classUser = User()
classUser.Policy = strPolicyName
strLevel = ''
if objOnCall['level'] == 1:
strLevel == 'Primary on call'
elif objOnCall['level'] == 2:
strLevel == 'Backup on call'
elif objOnCall['level'] == 3:
strLevel == 'Tetiary on call'
classUser.Level = strLevel
classUser.StartDate = getDate(objOnCall['start'])
classUser.EndDate = getDate(objOnCall['end'])
classUser.StartTime = getTime(objOnCall['start'])
classUser.EndTime = getTime(objOnCall['end'])
objUser = objOnCall['user']
classUser.Name = objUser['name']
classUser.Mobile = getUserMobile(objUser['id'])
lstUsers.append(classUser)
return render(request, 'oncall/rota.html', {'lstUsers': lstUsers, 'lstPolicy': lstPolicy})
HTML
<html>
<head>
<title>IT on call Rota</title>
</head>
<body>
<a href="/">Home</a>
<h2>1 - </h2>
<h2>1 - </h2>
<h2>1 - </h2>
<h2>1 - </h2>
<h2>1 - </h2>
</body>
</html>
无关的这个问题,但你会让你的代码稍微有点儿清洁,如果你可以通过objOnCall到你的用户构造函数 – SpoonMeiser
@alecxe添加基模板 – AlexW
@alecxe我不知道你的意思? ive添加了html来提问代码生成,它看起来像模板正在工作,但没有得到它的数据 – AlexW