Django v1.6调试工具栏中间件错误号.rsplit()

时间:2013-12-22 04:26:28

标签: django python-2.7 django-debug-toolbar django-1.6

我正在尝试将django-debug-toolbar与我的django应用程序一起使用,它适用于django v1.5。但是,我正在尝试将系统迁移到django v1.6,当我尝试加载任何页面时,它会生成以下错误。

python manage.py runserver
Validating models...

0 errors found
December 21, 2013 - 22:53:18
Django version 1.6.1, using settings 'MySite.settings'
Starting development server at http://XXX.XXX.XXX.XXX:XXXX/
Quit the server with CONTROL-C.
Internal Server Error: /
Traceback (most recent call last):
  File "/home/user/django-env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 90, in get_response
response = middleware_method(request)
  File "/home/user/django-env/local/lib/python2.7/site-packages/debug_toolbar/middleware.py", line 45, in process_request
mod_path, func_name = func_path.rsplit('.', 1)
AttributeError: 'function' object has no attribute 'rsplit'
[21/Dec/2013 22:53:21] "GET / HTTP/1.1" 500 58172

来源有这个说明,但我不确定它们的含义是什么(import_by_path):

def process_request(self, request):
    # Decide whether the toolbar is active for this request.
    func_path = dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK']
    # Replace this with import_by_path in Django >= 1.6.
    mod_path, func_name = func_path.rsplit('.', 1)
    show_toolbar = getattr(import_module(mod_path), func_name)
    if not show_toolbar(request):
        return

此外,我们正在努力。知道这个消息的意思吗?

/home/user/django-env/local/lib/python2.7/site-packages/debug_toolbar/settings.py:68: DeprecationWarning: SHOW_TOOLBAR_CALLBACK is now a dotted path. Update your DEBUG_TOOLBAR_CONFIG setting.
  "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)

我的settings.py:

def custom_show_toolbar(request):
    return True  # Always show toolbar, for example purposes only.

DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False,
    'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
    'INSERT_BEFORE': 'div',
    'ENABLE_STACKTRACES' : True,
}

1 个答案:

答案 0 :(得分:22)

问题由您收到的消息描述(不应该是弃用警告,而应该是TypeError,这可能是debug_toolbar中的错误):

  

SHOW_TOOLBAR_​​CALLBACK现在是一个虚线路径。更新您的DEBUG_TOOLBAR_​​CONFIG设置

SHOW_TOOLBAR_CALLBACK设置曾经是可调用的,但现在it is a dotted path是可调用的:字符串。您引用的代码会尝试rsplit SHOW_TOOLBAR_CALLBACK的值,但由于您无法rsplit函数对象,因此会出现错误。

要解决此问题,请将回调放入另一个python文件,例如your_site/toolbar_stuff.py,并将设置调整为'SHOW_TOOLBAR_CALLBACK': 'your_site.toolbar_stuff.custom_show_toolbar'。你可以通过尝试shell来测试它是否有效:

from your_site.toolbar_stuff import custom_show_toolbar

如果有效,您的新设置也应该有效。