调试工具栏 django 不执行
debug toolbar django not executing
我一直在尝试通过应用 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', #
'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']
知道为什么它不起作用吗?好像是在我的目录下加了双斜线,不知道为什么。
提前致谢。
根据 STATICFILES_DIRS 的 django 文档,即使在 windows 上,您也需要以 Unix 样式格式传递路径:
Note that these paths should use Unix-style forward slashes, even on
Windows (e.g. "C:/Users/user/mysite/extra_static_content").
解决方案
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static').replace("\", "/"),
]
我一直在尝试通过应用 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', #
'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']
知道为什么它不起作用吗?好像是在我的目录下加了双斜线,不知道为什么。
提前致谢。
根据 STATICFILES_DIRS 的 django 文档,即使在 windows 上,您也需要以 Unix 样式格式传递路径:
Note that these paths should use Unix-style forward slashes, even on Windows (e.g. "C:/Users/user/mysite/extra_static_content").
解决方案
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static').replace("\", "/"),
]