简体   繁体   中英

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. 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; 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. 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; it adds nothing to encode that as part of the variable name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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