0

我在理解如何在本地和 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">
4

1 回答 1

0

我有一个类似的问题,我发现当我在我的问题中发表评论时'django.contrib.staticfiles'INSTALLED_APPS我的问题得到了解决。

于 2019-12-05T18:59:30.017 回答