python - 使用 Whitenoise 在本地和 Heroku 上调试时出现错误 500
问题描述
我在理解如何在本地和 heroku 上正确托管我的静态文件时遇到了很多麻烦。目前,当 Debug 关闭时,我收到错误 500- 否则它可以正常工作。
我已经阅读了很多 SO 帖子,但到目前为止,没有任何解决方案有帮助。我也尝试在 AWS 上托管静态文件,但这仍然没有帮助。
编辑 我目前在除管理控制台之外的任何页面上都收到 500 错误。
来自 django 服务器的错误:
[27/Nov/2019 13:36:07] "GET / HTTP/1.1" 500 9061
来自 Web 控制台的错误:
Failed to load resource: localhost1/: the server responded with a status of 500 (Internal Server Error)
下面是我的代码:
设置.py
INSTALLED_APPS = [
'widget_tweaks',
'django_select2',
'masterform',
'tempus_dominus',
'team',
'sms',
'opportunities',
"rest_framework",
'import_export',
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, "live-static", "static-root")
网址.py
from django.views.static import serve
from bdsurveyapp import settings
urlpatterns = [
url('', include('opportunities.urls')),
url(r'^sms/', include('sms.urls')),
url(r'^admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
url(r'^dashboard/', include('dashboard.urls')),
path('login/', auth_views.LoginView.as_view(template_name='pages/login.html'), name="login"),
path('logout/', auth_views.LogoutView.as_view(template_name='pages/logout.html'), name="logout"),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
部分base.html
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
{{ form.media.css }}
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="apple-touch-icon" sizes="180x180" href="{% static 'img/apple-touch-icon.png' %}">
<link rel="icon" type="image/png" sizes="32x32" href="{% static 'img/favicon-32x32.png' %}">
<link rel="icon" type="image/png" sizes="16x16" href="{% static 'img/favicon-16x16.png' %}">
<link rel="manifest" href="{% static 'img/site.webmanifest' %}">
<link rel="mask-icon" href="{% static 'img/safari-pinned-tab.svg' %}" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
解决方案
我有一个类似的问题,我发现当我在我的问题中发表评论时'django.contrib.staticfiles'
,INSTALLED_APPS
我的问题得到了解决。
推荐阅读
- python - 使用基于另一列的条件从熊猫数据框中删除重复项
- python - 如何使用布尔值匹配两列
- javascript - 将新属性添加到 react-leaflet 标记组件的 TypeScript 问题
- php - PHP HttpError 响应
- flutter - 在一个简单的 BLoC 示例中使用 Streams/Sinks
- javascript - 是否可以隐藏缺少的字体字符?
- excel - Excel - Trying to compare two columns worth of data in separate sheets and if they match return a set of data from each sheet
- python - Django: What do I need to do to get the csv file to actually load into the model?
- jmeter - Jmeter - How to stop While Controller once getting the expected values from Data Base?
- php - Symfony 5: I can't login after logout