【问题标题】:Django v1.6 debug-toolbar Middleware Error No .rsplit()Django v1.6 调试工具栏中间件错误号 .rsplit()
【发布时间】:2014-01-10 15:38:39
【问题描述】:

我正在尝试将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)

我的设置.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,
}

【问题讨论】:

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


    【解决方案1】:

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

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

    SHOW_TOOLBAR_CALLBACK 设置曾经是可调用的,但现在 it is a dotted path 设置为可调用的:字符串。你引用的代码试图rsplitSHOW_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
    

    如果这样有效,那么您的新设置也应该有效。

    【讨论】:

    • 嗯...我实际上在发布之前尝试了您建议的一些事情-我查看了文档,但对我来说不是很清楚,错误也不清楚,并且代码中的注释具有误导性。我还尝试对 MySite.settings.custom_show_toolbar 使用虚线路径,但这不起作用。最终的解决方案是使用点分路径,但将其封装为字符串。例如,'SHOW_TOOLBAR_CALLBACK': 'MySite.settings.custom_show_toolbar'。或者,您可以将该功能移动到外部配置文件,但我将其保留在设置中。感谢您的反馈,它使我找到了正确的解决方案!
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多