无法使用Django debug-toolbar 1.9来处理django 2.0

时间:2017-12-28 12:20:28

标签: django

我已经使用pip安装了Django调试工具栏,并添加了' debug_toolbar'到安装应用程序列表。

当我运行服务器时,工具栏不会显示。我错过了什么步骤?!

1 个答案:

答案 0 :(得分:1)

installation guide之后的2.0.7中对我有用的是:

1)安装新的 django 调试工具栏

pip install -U django
pip install django-debug-toolbar

2) settings.py1.x迁移到2.x

我创建了一个虚拟项目:django-admin startproject mysite,并为mysite\mysite\settings.pyINSTALLED_APPS签出了默认的MIDDLEWARE。剩下的就是添加debug_toolbar

# As mentioned in the last step of Installation Guide
INTERNAL_IPS = ['127.0.0.1', '::1']

# Default config + some changes
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'debug_toolbar',     # Add this
    'mysite',            # Your app
]

# Default config + some changes
MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware', # Add this
    'django.middleware.security.SecurityMiddleware',
    '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',
]

3)添加URL

再次,只需遵循Installation Guide并将其添加到URL配置中即可:

from django.conf import settings
from django.conf.urls import include, url

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns