Django调试“索引错误:列表索引超出范围”

0 投票
1 回答
2709 浏览
提问于 2025-04-17 08:50

我在做一个django项目,突然之间,django的调试功能就不工作了,显示了一个异常,而不是我代码中的错误追踪信息。

这个异常看起来是调试器本身的问题,很多地方都讨论过,表示这可能和Mac上的换行符有关(在这里),还有可能是堆栈中的行和磁盘上的代码不匹配(在这里)。

有趣的是,这个异常只在不是因为语法错误的情况下发生。如果我随便加一些错误的语法,django会正常显示调试界面,提示“无效的语法(views.py,第xyz行)”。

我使用的是Django 1.3,Python 2.7,Ubuntu 11.04。这个项目有几千行代码,所以在这里复制粘贴很困难。

有没有人也遇到过这个问题,知道还有哪些因素可能导致这个异常,以及如何解决它?

Traceback (most recent call last):

 File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 283, in run
  self.result = application(self.environ, self.start_response)

 File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
  return self.application(environ, start_response)

 File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 273, in __call__
  response = self.get_response(request)

 File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 169, in get_response
  response = self.handle_uncaught_exception(request, resolver, sys.exc_info())

 File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
  return debug.technical_500_response(request, *exc_info)

 File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 59, in technical_500_response
  html = reporter.get_traceback_html()

 File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 114, in get_traceback_html
  frames = self.get_traceback_frames()

 File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 242, in get_traceback_frames
  pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)

 File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 223, in _get_lines_from_file
  context_line = source[lineno].strip('\n')

 IndexError: list index out of range

1 个回答

0

我没有找到问题的解决办法,但我找到了一种方法,可以通过使用不同的调试工具来获得更详细的错误信息。

你可以去这里看看: https://github.com/dcramer/django-devserver

- 首先在终端里运行 pip install 来安装 devserver。

- 然后再运行 pip install 来安装 werkzeug。

- 接着在你的 settings.py 文件中,把 dev-server 加入到你的应用列表里。

- 最后,用命令 ./manage.py runserver --werkzeug 启动 Django。

撰写回答