Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Django-函数的列表索引超出范围_Python_Django - Fatal编程技术网

Python/Django-函数的列表索引超出范围

Python/Django-函数的列表索引超出范围,python,django,Python,Django,我有一个函数,可以获取用户历史记录并将其放入面包屑中,我正试图通过显示来使面包屑看起来更好。因此,我的函数应该能够返回一个可读性很好的url,而不是/service/site/7,它会将“site UK”或“Showroom-Italy”作为面包屑 但是,当我加载的页面没有(我认为)足够的项目时,我会收到错误“异常值:列表索引超出范围” 我认为通过使用“if list_path[1]:”如果该项不存在,它将返回false,但是它仍然会给我错误 我还尝试使用if“list\u path[0]>0”

我有一个函数,可以获取用户历史记录并将其放入面包屑中,我正试图通过显示来使面包屑看起来更好。因此,我的函数应该能够返回一个可读性很好的url,而不是/service/site/7,它会将“site UK”或“Showroom-Italy”作为面包屑

但是,当我加载的页面没有(我认为)足够的项目时,我会收到错误“异常值:列表索引超出范围”

我认为通过使用“if list_path[1]:”如果该项不存在,它将返回false,但是它仍然会给我错误

我还尝试使用if“list\u path[0]>0”和“if list\u path[0]>1”加载一些页面,但其他页面加载失败,出现相同错误

我认为这是因为它存储了4个值,并且每次都在所有4个值上运行脚本

谢谢

代码:

回溯:

Environment:


Request Method: GET
Request URL: http://it.internal.com/service/showrooms

Django Version: 1.9.6
Python Version: 2.7.5
Installed Applications:
('home.apps.HomeConfig',
 'oncall.apps.OncallConfig',
 'networks.apps.NetworksConfig',
 'maintenance.apps.MaintenanceConfig',
 'service.apps.ServiceConfig',
 'management.apps.ManagementConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'debug_toolbar',
 'twitter_bootstrap',
 'bootstrap_pagination')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')



Traceback:

File "/usr/lib64/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/usr/lib64/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib64/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/var/www/infternal/service/views.py" in showroom_list
  41.         'Showrooms': modelShowrooms,

File "/usr/lib64/python2.7/site-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "/usr/lib64/python2.7/site-packages/django/template/loader.py" in render_to_string
  97.         return template.render(context, request)

File "/usr/lib64/python2.7/site-packages/django/template/backends/django.py" in render
  95.             return self.template.render(context)

File "/usr/lib64/python2.7/site-packages/django/template/base.py" in render
  204.                 with context.bind_template(self):

File "/usr/lib64/python2.7/contextlib.py" in __enter__
  17.             return self.gen.next()

File "/usr/lib/python2.7/site-packages/debug_toolbar/panels/templates/panel.py" in _request_context_bind_template
  79.             context = processor(self.request)

File "/var/www/infternal/infternal/context_processors.py" in breadcrumb_history
  52.         if list_path[1]:

Exception Type: IndexError at /service/showrooms
Exception Value: list index out of range
编辑: 示例URL包括:

/ (this would be replaced with home)
/majorsite/2 (replaced with "Major Site - HQ" or "Major Site - Warehouse")
/showroom/7 (replaced with "Showroom - London" or "Showroom - Manchester")
你应该使用

try: 
    ...
expect IndexError:
    # Handle exception

列表路径
一无所有时,您的URL似乎并不总是如您所期望的那样,这涉及到有时在
列表路径
中没有多个项目,这可能是由于
/majorsite
/admin
等路径造成的。。。因此,按照更安全的解决方案进行更换可能更准确:

if list_path[0]:
    if list_path[0] != "admin":
        del list_path[0]  # Here you delete the first element of the list.
                          # Thus `list_path` now equals `['showrooms']`.
                          # and `list_path[1]` doesn't exist anymore.
to_replace = ['majorsite', ...]
replace_with = ['Major Site', ...]

for item in zip(to_replace,replace_with):
    if item[0] in list_path:
        # replace item[0] by item[1] in list_path, since you are sure it is there
        # add additional customizations
这显然是一种替代方法,您可以按照自己的方式进行操作,但在这种情况下,您需要确保,在每种情况下,在请求索引之前,数组都具有预期的长度,而且,要替换的术语也存在:

if len(list_path)>1 and list_path[1]:
    # what you do today
else:
    # handle other cases

另外,您的代码有点复杂,我几乎可以肯定有一种简单的方法可以实现您想要的。如果你添加一些你可以在那里处理的所有可能URL的示例,以及每种情况下的预期结果,你会在这里得到一些好的简单的答案。

你能添加整个回溯吗?为什么不使用
try-except
块而不是
If
?这是正确的,但是它没有解释为什么
列出路径[1]
抛出索引器,而
列表路径应该是
['service','showrooms']
。嗨,我添加了一些示例URL,如果有帮助的话?谢谢
if len(list_path)>1 and list_path[1]:
    # what you do today
else:
    # handle other cases