简体   繁体   English

Python,Django-“列表”对象没有属性“推”

[英]Python, Django - 'list' object has no attribute 'push'

im trying to pass two lists through to a django template and the run some logic on them to get my desired output. 我试图将两个列表传递给django模板,并对它们运行一些逻辑以获取所需的输出。 however when i try load the page, im getting the below error. 但是,当我尝试加载页面时,我收到以下错误。

can anyone help me out? 谁能帮我吗? Thanks 谢谢

error 错误

Environment:


Request Method: GET
Request URL: http://10.66.118.55/oncall/

Django Version: 1.9.5
Python Version: 2.7.11
Installed Applications:
['oncall.apps.OncallConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/dist-packages/Django-1.9.5-py2.7.egg/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/Django-1.9.5-py2.7.egg/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/aw/INFTERNAL/oncall/views.py" in index
  126.     return render(request, 'oncall/rota.html', lstUsers, lstPolicy)

File "/usr/local/lib/python2.7/dist-packages/Django-1.9.5-py2.7.egg/django/shortcuts.py" in render
  89.             using=using)

File "/usr/local/lib/python2.7/dist-packages/Django-1.9.5-py2.7.egg/django/template/loader.py" in render_to_string
  114.                         template_name, context, context_instance, dirs, dictionary)

File "/usr/local/lib/python2.7/dist-packages/Django-1.9.5-py2.7.egg/django/template/engine.py" in render_to_string
  243.         with context_instance.push(context):

Exception Type: AttributeError at /oncall/
Exception Value: 'list' object has no attribute 'push'

view 视图

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, lstPolicy)

template 模板

{% extends 'oncall/base.html' %}

{% block content %}
    {% for pol in lstPolicy %}
        <h2>{{ pol.Name {}</h2>
        {% for user in lstUsers %}  
            {% if user.Policy == pol.Name %}
                <h3>{{ user.Level }}</h3>
                <p> 
                    Mobile: {{ user.Mobile }
                    From: {{ user.StartTime }} on {{ user.StartDate }}
                    Until: {{ user.EndTime }} on {{ user.EndDate }}
                </p>
            {% endif %} 
        {% endfor %}
    {% endfor %}
{% endblock %}

The third argument to render should be a dictionary mapping names to values; render的第三个参数应该是将名称映射到值的字典。 currently you are just passing the values themselves. 当前,您只是在传递值本身。

return render(request, 'oncall/rota.html', {'lstUsers': lstUsers, 'lstPolicy': lstPolicy})

Note that there doesn't really seem to be any point to the User class that you previously asked about. 请注意,您之前询问的User类似乎没有任何意义。 It would be much simpler and clearer to pass the user data as a list of dictionaries. 将用户数据作为字典列表传递会更加简单明了。

And seriously, drop the Hungarian notation. 并认真地放弃匈牙利符号。 It is obvious from the code that lstUsers is a list; 从代码中可以明显看出lstUsers是一个列表。 it adds nothing to encode that as part of the variable name. 它没有添加任何内容来将其编码为变量名的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM