导致 django 500 错误的静态文件问题?
static file issue causing django 500 error?
我正在使用 angular heroku 和 django。我一直注意到,当我使用 'ng build' 构建新的静态文件以添加到 django 然后推送到 heroku 时,heroku 实例显示的网站比我当前的代码落后几个版本。
我今天尝试 运行 我的 django 本地服务器 ngbuild
将我的文件放在指定的文件夹中,
运行宁python manage.py collectstatic
它 运行 成功了。
然后我 运行 我的 django 服务器,导航到我的页面,我收到 500 响应。
因为我正在使用 angular,所以我已经将我的 django 服务器设置为休息后端。
其余服务使用的每个端点都以 url api/
开头
所以localdomain/api/ <-- restful 服务
localdomain 单独服务于 angular 应用程序。
我只在尝试获取应用程序服务时收到 500 服务器错误。
以下是我对静态文件的所有设置:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
#angular distro root
ANGULAR_APP_DIR = os.path.join(BASE_DIR, 'frontend/dist/')
#image distro root
ASSETS_DIR = os.path.join(BASE_DIR, 'frontend/dist/assets/')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
#STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(ANGULAR_APP_DIR),
os.path.join(ASSETS_DIR),
]
我的模板设置:
ROOT_URLCONF = 'suitsandtables.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['suitsandtables/templates',
'stemail/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',
],
},
},
]
我的 url 提供静态文件,我的 url 呈现 index.html 页面
url(r'^(?!/?static/)(?!/?media/)(?P<path>.*\..*)$',
RedirectView.as_view(url='/static/%(path)s', permanent=False)),
url(r'^$', views.RootView.as_view()),
我的根视图 class 渲染 index.html
class RootView(TemplateView):
def get(self, request, **kwargs):
return render(request, 'index.html', context = None)
这是白噪声的常见问题。我已经在这上面浪费了无数个小时。以下是处理此问题的方法。
把这两行放在你的 settings.py
DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = True
并尝试 运行 heroku 上的应用程序。它会像往常一样破裂。
现在去
https://dashboard.heroku.com/apps/<app_name>/logs
通常,当 debug 为 False 时,您将看不到日志,但是由于 DEBUG_PROPAGATE_EXCEPTIONS=True,您可以查看日志
在那里,寻找whitenoise找不到的文件,让whitenoise失控。在我的例子中,它是一些随机的 css 文件,它在我的 base.html 中被引用。
您可以修复它的位置,或者直接从 base.html 中删除引用此文件的行。
在此之后,如果其他文件有问题,请继续这样做。
whitenoise最终会快乐,你也会快乐。
我正在使用 angular heroku 和 django。我一直注意到,当我使用 'ng build' 构建新的静态文件以添加到 django 然后推送到 heroku 时,heroku 实例显示的网站比我当前的代码落后几个版本。
我今天尝试 运行 我的 django 本地服务器 ngbuild
将我的文件放在指定的文件夹中,
运行宁python manage.py collectstatic
它 运行 成功了。
然后我 运行 我的 django 服务器,导航到我的页面,我收到 500 响应。
因为我正在使用 angular,所以我已经将我的 django 服务器设置为休息后端。
其余服务使用的每个端点都以 url api/
开头所以localdomain/api/ <-- restful 服务
localdomain 单独服务于 angular 应用程序。
我只在尝试获取应用程序服务时收到 500 服务器错误。
以下是我对静态文件的所有设置:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
#angular distro root
ANGULAR_APP_DIR = os.path.join(BASE_DIR, 'frontend/dist/')
#image distro root
ASSETS_DIR = os.path.join(BASE_DIR, 'frontend/dist/assets/')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
#STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(ANGULAR_APP_DIR),
os.path.join(ASSETS_DIR),
]
我的模板设置:
ROOT_URLCONF = 'suitsandtables.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['suitsandtables/templates',
'stemail/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',
],
},
},
]
我的 url 提供静态文件,我的 url 呈现 index.html 页面
url(r'^(?!/?static/)(?!/?media/)(?P<path>.*\..*)$',
RedirectView.as_view(url='/static/%(path)s', permanent=False)),
url(r'^$', views.RootView.as_view()),
我的根视图 class 渲染 index.html
class RootView(TemplateView):
def get(self, request, **kwargs):
return render(request, 'index.html', context = None)
这是白噪声的常见问题。我已经在这上面浪费了无数个小时。以下是处理此问题的方法。
把这两行放在你的 settings.py
DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = True
并尝试 运行 heroku 上的应用程序。它会像往常一样破裂。
现在去
https://dashboard.heroku.com/apps/<app_name>/logs
通常,当 debug 为 False 时,您将看不到日志,但是由于 DEBUG_PROPAGATE_EXCEPTIONS=True,您可以查看日志
在那里,寻找whitenoise找不到的文件,让whitenoise失控。在我的例子中,它是一些随机的 css 文件,它在我的 base.html 中被引用。
您可以修复它的位置,或者直接从 base.html 中删除引用此文件的行。
在此之后,如果其他文件有问题,请继续这样做。
whitenoise最终会快乐,你也会快乐。