2016-04-28 322 views
0

即时通讯尝试将两个列表传递给一个Django模板并运行一些逻辑来获取我想要的输出。然而,当我尝试加载页面,即时获取下面的错误。Python,Django - 'list'对象没有属性'push'

任何人都可以帮我吗? 由于

错误

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' 

视图

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) 

模板

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

请显示完整的回溯。在这段代码中,你似乎没有在任何地方调用'push'。 –

+0

@DanielRoseman对不起,我没有意识到我需要什么,现在加入 – AlexW

+0

你的函数render(request,'oncall/rota.html',lstUsers,lstPolicy)的第三个参数是错误的。请再次阅读文档[一个快捷方式渲染](https://docs.djangoproject.com/en/1.9/intro/tutorial03/#a-shortcut-render) – qvpham

回答

2

的第三个参数来渲染应该是一个字典映射名称值;目前你只是自己传递价值观。

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

请注意,您之前询问的用户类似乎没有任何意义。将用户数据作为字典列表传递将会更加简单和清晰。

严重的是,放弃匈牙利符号。从代码可以看出,lstUsers是一个列表;它没有增加任何东西来编码,作为变量名称的一部分。

+0

谢谢你现在经历了,我想我有另一个问题值,页面是空的,pol.Name是访问值的正确方法?谢谢 – AlexW

相关问题