django访问模板中的列表中的值

时间:2022-03-17 20:21:03

My web page when viewed is currently blank, looking at other samples it seems i am accessing the values correctly but im not getting any data. i have checked my lists by printing them, and they both have plenty of data in them

我的网页现在是空白的,看看其他的样本,我似乎正在正确地访问这些值,但是我没有得到任何数据。我已经打印了我的列表,它们都有大量的数据

template

模板

{% 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 %}

base template

基本模板

{% load staticfiles %}
<html>
    <head>
        <title>IT on call Rota</title>
    </head>
    <body>
        <a href="/">Home</a>
        {% block content %}
        {% endblock %}
    </body>
</html>

view

视图

# 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 generated

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>

1 个解决方案

#1


1  

You add strPolicyName :

你添加strPolicyName:

lstPolicy.append(strPolicyName) 

but you must add classPolicy:

但是你必须加上classPolicy:

lstPolicy.append(classPolicy) 

A string doesn't have Name property :)

字符串没有名称属性:)

#1


1  

You add strPolicyName :

你添加strPolicyName:

lstPolicy.append(strPolicyName) 

but you must add classPolicy:

但是你必须加上classPolicy:

lstPolicy.append(classPolicy) 

A string doesn't have Name property :)

字符串没有名称属性:)