我一直在尝试通过应用docs使用django中的调试工具栏。
在INTERNAL_IPS=['127.0.0.1']
上添加settings.py
之前,可以在本地执行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://stackoverflow.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']
有人知道为什么它不起作用吗?似乎在目录中添加了双斜杠,但我不知道为什么。
谢谢。
答案 0 :(得分:2)
根据STATICFILES_DIRS的django文档,即使在Windows上,也需要以Unix格式传递路径:
请注意,这些路径应使用Unix风格的正斜杠,即使在 Windows(例如“ C:/ Users / user / mysite / extra_static_content”)。
解决方案
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static').replace("\\", "/"),
]