为什么在调试模式下 app/admin false 给我服务器错误 500
Why app/admin in debug mode false give me Server Error 500
我将服务器 settings.py 从 Debug = True
更改为 Debug = False
。
我可以从我的其他应用 url 获取 httpResponses
,但是 我无法访问 /admin url 具体 ,我获取服务器错误 (500)
我尝试按照此问题的其他主题所述更改 ALLOWED_HOSTS = ['my-app-name.herokuapp.com','*']
,但对我的情况没有帮助。
(添加'*'只是为了这个问题,以表明我已经尝试过但没有解决问题)
编辑 1(添加设置):
settings.py
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.10.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ADMINS = [('admin', 'adminsomthingsomewhere@gmail.com')]
MANAGERS = ADMINS
ALLOWED_HOSTS = ['mysitename.herokuapp.com', 'localhost', '127.0.0.1','*']
# Application definition
INSTALLED_APPS = [
'Myappname.apps.MyAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.admindocs',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
]
ROOT_URLCONF = 'Mysiteproj.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'Mysiteproj.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
# Update database configuration with $DATABASE_URL.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Password validation
# https://docs.djangoproject.com/en/1.10/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',
},
]
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
编辑 2(添加日志):
我在创建日志文件时遇到了问题。现在我设法配置它,这是我尝试访问管理站点时的日志跟踪。
Internal Server Error: /admin/login/
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 215, in _get_response
response = response.render()
File "c:\Python27\lib\site-packages\django\template\response.py", line 109, in render
self.content = self.rendered_content
File "c:\Python27\lib\site-packages\django\template\response.py", line 86, in rendered_content
content = template.render(context, self._request)
File "c:\Python27\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 208, in render
return self._render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\template\loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\template\loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\template\loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\templatetags\static.py", line 104, in render
url = self.url(context)
File "c:\Python27\lib\site-packages\django\templatetags\static.py", line 101, in url
return self.handle_simple(path)
File "c:\Python27\lib\site-packages\django\templatetags\static.py", line 114, in handle_simple
return staticfiles_storage.url(path)
File "c:\Python27\lib\site-packages\django\contrib\staticfiles\storage.py", line 132, in url
hashed_name = self.stored_name(clean_name)
File "c:\Python27\lib\site-packages\django\contrib\staticfiles\storage.py", line 292, in stored_name
cache_name = self.clean_name(self.hashed_name(name))
File "c:\Python27\lib\site-packages\django\contrib\staticfiles\storage.py", line 95, in hashed_name
(clean_name, self))
ValueError: The file 'admin/css/base.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x0465BAF0>.
"GET /admin/login/?next=/admin/ HTTP/1.1" 500 27
为什么屏蔽我?我该如何解决?谢谢。
指定 ADMINS
设置,以便您将在生产模式下收到有关 500 错误的电子邮件:
ADMINS = (
('Your name', 'your@email.com'),
)
文档:https://docs.djangoproject.com/en/1.10/ref/settings/#admins
还要确保 EMAIL_HOST
、EMAIL_HOST_USER
和 EMAIL_HOST_PASSWORD
已设置。
您还应该在 Heroku 上的应用程序日志中看到真正的错误。
这个问题似乎与静态资产的访问有关。
似乎发布了类似的问题:
Django staticfiles not found on Heroku (with whitenoise)
这个问题似乎可以通过更改静态资产的配置来解决。
在查看您的日志时,我提出了一个可能是您的问题的建议。
加载管理页面时,Django 使用它自己的静态文件来向您显示正确的 HTML/JS/CSS 您看到的网站作为模型的管理页面。
所有这些文件都来自 Django 库。当你说你没有调试你的服务器(DEBUG=FALSE)时,Django 想要加载所有资产(HTML,JS,CSS 文件)运行 环境项目而不是库 itself.He 这样做是因为他依赖于开发人员调用 collectstatic 命令,该命令需要正确且有效的静态文件配置。
顺便说一句,当我说正确的配置时,我指的是存在的目录和路径。
此外,如果加载静态文件(如 jQuery、Bootstrap 等文件)的配置错误,则无法加载它们。 ..
我通过配置和其他评论看到您正在使用 Heroku。
Heroku 默认情况下会自动为您调用 collectstatic 命令,当然他需要收集所有静态文件(包括 ADMIN 文件....)
Link 参考 - Heroku - Django and Static Assets
到目前为止我们知道的事情很少:
- 不仅我们的文件需要静态文件配置,管理页面也需要静态文件配置
- 当您在 Heroku 上构建 Django 应用程序时,您会自动执行 collectstatic,但如果配置无效,它将无法正常工作。
- 在您发布的日志中,我看到您无法访问管理页面的 css 文件,因此对于我所描述的内容来说,这是一个红灯..
我猜测您的静态文件配置有误,并且您从未在本地计算机上调用或检查过 collectstatic 命令。
这意味着您正在开发一个 REST api 服务器,可以访问管理页面来监控您的数据库...当您以这种方式使用 Django 而不是开发网站时,通常会发生这种情况。
我并不是暗示用 Django 或任何东西开发 REST api 是错误的,我只是想让一切都清楚
尝试将此代码段用于您的静态文件配置:
PROJECT_ROOT = os.path.dirname(os.path.abspath(_file_))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
我希望这个答案对你有用,如果有用请告诉我。
也遇到了这个问题,但解决方案在于从静态位置配置 nginx。应该是
location /static/ {
root /var/www/fancy-project/;
}
而不是
location /static/ {
root /var/www/fancy-project/static/;
}
也许这对某人有用
我将服务器 settings.py 从 Debug = True
更改为 Debug = False
。
我可以从我的其他应用 url 获取 httpResponses
,但是 我无法访问 /admin url 具体 ,我获取服务器错误 (500)
我尝试按照此问题的其他主题所述更改 ALLOWED_HOSTS = ['my-app-name.herokuapp.com','*']
,但对我的情况没有帮助。
(添加'*'只是为了这个问题,以表明我已经尝试过但没有解决问题)
编辑 1(添加设置):
settings.py
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.10.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ADMINS = [('admin', 'adminsomthingsomewhere@gmail.com')]
MANAGERS = ADMINS
ALLOWED_HOSTS = ['mysitename.herokuapp.com', 'localhost', '127.0.0.1','*']
# Application definition
INSTALLED_APPS = [
'Myappname.apps.MyAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.admindocs',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
]
ROOT_URLCONF = 'Mysiteproj.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'Mysiteproj.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
# Update database configuration with $DATABASE_URL.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Password validation
# https://docs.djangoproject.com/en/1.10/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',
},
]
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
编辑 2(添加日志):
我在创建日志文件时遇到了问题。现在我设法配置它,这是我尝试访问管理站点时的日志跟踪。
Internal Server Error: /admin/login/
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "c:\Python27\lib\site-packages\django\core\handlers\base.py", line 215, in _get_response
response = response.render()
File "c:\Python27\lib\site-packages\django\template\response.py", line 109, in render
self.content = self.rendered_content
File "c:\Python27\lib\site-packages\django\template\response.py", line 86, in rendered_content
content = template.render(context, self._request)
File "c:\Python27\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 208, in render
return self._render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\template\loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\template\loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\template\loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 994, in render
bit = node.render_annotated(context)
File "c:\Python27\lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
File "c:\Python27\lib\site-packages\django\templatetags\static.py", line 104, in render
url = self.url(context)
File "c:\Python27\lib\site-packages\django\templatetags\static.py", line 101, in url
return self.handle_simple(path)
File "c:\Python27\lib\site-packages\django\templatetags\static.py", line 114, in handle_simple
return staticfiles_storage.url(path)
File "c:\Python27\lib\site-packages\django\contrib\staticfiles\storage.py", line 132, in url
hashed_name = self.stored_name(clean_name)
File "c:\Python27\lib\site-packages\django\contrib\staticfiles\storage.py", line 292, in stored_name
cache_name = self.clean_name(self.hashed_name(name))
File "c:\Python27\lib\site-packages\django\contrib\staticfiles\storage.py", line 95, in hashed_name
(clean_name, self))
ValueError: The file 'admin/css/base.css' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x0465BAF0>.
"GET /admin/login/?next=/admin/ HTTP/1.1" 500 27
为什么屏蔽我?我该如何解决?谢谢。
指定 ADMINS
设置,以便您将在生产模式下收到有关 500 错误的电子邮件:
ADMINS = (
('Your name', 'your@email.com'),
)
文档:https://docs.djangoproject.com/en/1.10/ref/settings/#admins
还要确保 EMAIL_HOST
、EMAIL_HOST_USER
和 EMAIL_HOST_PASSWORD
已设置。
您还应该在 Heroku 上的应用程序日志中看到真正的错误。
这个问题似乎与静态资产的访问有关。
似乎发布了类似的问题: Django staticfiles not found on Heroku (with whitenoise)
这个问题似乎可以通过更改静态资产的配置来解决。
在查看您的日志时,我提出了一个可能是您的问题的建议。
加载管理页面时,Django 使用它自己的静态文件来向您显示正确的 HTML/JS/CSS 您看到的网站作为模型的管理页面。
所有这些文件都来自 Django 库。当你说你没有调试你的服务器(DEBUG=FALSE)时,Django 想要加载所有资产(HTML,JS,CSS 文件)运行 环境项目而不是库 itself.He 这样做是因为他依赖于开发人员调用 collectstatic 命令,该命令需要正确且有效的静态文件配置。
顺便说一句,当我说正确的配置时,我指的是存在的目录和路径。
此外,如果加载静态文件(如 jQuery、Bootstrap 等文件)的配置错误,则无法加载它们。 ..
我通过配置和其他评论看到您正在使用 Heroku。 Heroku 默认情况下会自动为您调用 collectstatic 命令,当然他需要收集所有静态文件(包括 ADMIN 文件....) Link 参考 - Heroku - Django and Static Assets
到目前为止我们知道的事情很少:
- 不仅我们的文件需要静态文件配置,管理页面也需要静态文件配置
- 当您在 Heroku 上构建 Django 应用程序时,您会自动执行 collectstatic,但如果配置无效,它将无法正常工作。
- 在您发布的日志中,我看到您无法访问管理页面的 css 文件,因此对于我所描述的内容来说,这是一个红灯..
我猜测您的静态文件配置有误,并且您从未在本地计算机上调用或检查过 collectstatic 命令。 这意味着您正在开发一个 REST api 服务器,可以访问管理页面来监控您的数据库...当您以这种方式使用 Django 而不是开发网站时,通常会发生这种情况。
我并不是暗示用 Django 或任何东西开发 REST api 是错误的,我只是想让一切都清楚
尝试将此代码段用于您的静态文件配置:
PROJECT_ROOT = os.path.dirname(os.path.abspath(_file_))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
我希望这个答案对你有用,如果有用请告诉我。
也遇到了这个问题,但解决方案在于从静态位置配置 nginx。应该是
location /static/ {
root /var/www/fancy-project/;
}
而不是
location /static/ {
root /var/www/fancy-project/static/;
}
也许这对某人有用