调试工具栏 django 未执行

Posted

技术标签:

【中文标题】调试工具栏 django 未执行【英文标题】:debug toolbar django not executing 【发布时间】:2019-06-05 17:26:56 【问题描述】:

我一直在尝试通过应用docs 来使用 django 的调试工具栏。 在settings.py上添加INTERNAL_IPS=['127.0.0.1']之前,django可以在本地执行,我可以运行它,但是工具栏没有出现。

添加INTERNAL_IPS=['127.0.0.1'] 后,出现以下错误:

Internal Server Error: /
Traceback (most recent call last):
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\utils\deprecation.py", line 97, in __call__
    response = self.process_response(request, response)
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\debug_toolbar\middleware.py", line 133, in process_response
    panel.generate_stats(request, response)
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\debug_toolbar\panels\staticfiles.py", line 136, in generate_stats
    "staticfiles_finders": self.get_staticfiles_finders(),
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\debug_toolbar\panels\staticfiles.py", line 148, in get_staticfiles_finders
    for path, finder_storage in finder.list([]):
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\contrib\staticfiles\finders.py", line 125, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\contrib\staticfiles\utils.py", line 28, in get_files
    directories, files = storage.listdir(location)
  File "C:\Anaconda3\envs\mydjan\lib\site-packages\django\core\files\storage.py", line 313, in listdir
    for entry in os.listdir(path):
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Alvaro Lloret\\Home\\alpha_trader\\static\\'
[10/Jan/2019 20:14:30] "GET / HTTP/1.1" 500 94293

在此找到我的 settings.py:

import os

from django.contrib.messages import constants as messages

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'n2th-ublablalablablals'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    # 'django.contrib.admin', # https://***.com/a/44651902/5176549
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'debug_toolbar',
    'django.contrib.humanize',
    'crispy_forms',
    'accounts'
]

MIDDLEWARE = [
    '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',

    'debug_toolbar.middleware.DebugToolbarMiddleware',
]

ROOT_URLCONF = 'alpha_trader.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': 
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        ,
    ,
]

WSGI_APPLICATION = 'alpha_trader.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    



# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    ,
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]



# Custom Django auth settings

AUTH_USER_MODEL = 'accounts.User'

LOGIN_URL = 'login'

LOGOUT_URL = 'logout'

LOGIN_REDIRECT_URL = 'home'

LOGOUT_REDIRECT_URL = 'home'


# Messages built-in framework

MESSAGE_TAGS = 
    messages.DEBUG: 'alert-secondary',
    messages.INFO: 'alert-info',
    messages.SUCCESS: 'alert-success',
    messages.WARNING: 'alert-warning',
    messages.ERROR: 'alert-danger',


# Third party apps configuration

CRISPY_TEMPLATE_PACK = 'bootstrap4'

INTERNAL_IPS=['127.0.0.1']

知道为什么它不起作用吗?它似乎在我的目录上添加了双斜杠,但我不知道为什么。

提前谢谢你。

【问题讨论】:

您的 STATIC_ROOT 设置的值是多少? 我没有修改,所以默认的。我应该改变它吗? 你用的是什么版本的python和django? Python 3.7.1(默认,2018 年 12 月 10 日,22:54:23),Django==2.0.2 【参考方案1】:

根据STATICFILES_DIRS 的 django 文档,即使在 Windows 上,您也需要以 Unix 样式格式传递路径:

请注意,这些路径应使用 Unix 样式的正斜杠,即使在 Windows(例如“C:/Users/user/mysite/extra_static_content”)。

解决方案

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static').replace("\\", "/"),
]

【讨论】:

以上是关于调试工具栏 django 未执行的主要内容,如果未能解决你的问题,请参考以下文章

Django 调试工具栏安装(Django 1.9)

如何在 Django 调试工具栏中使用日志记录?

Django 调试工具栏显示在根 URL 中,但不在应用 URL 中

Django之Django debug toolbar调试工具

Django django-debug-toolbar调试工具配置

Django 开发调试工具:Django-debug-toolbar