【问题标题】:static file issue causing django 500 error?导致 django 500 错误的静态文件问题?
【发布时间】:2018-12-09 00:23:34
【问题描述】:

我正在使用 angular heroku 和 django。我一直注意到,当我使用“ng build”构建新的静态文件以添加到 django 然后推送到 heroku 时,heroku 实例显示的网站比我当前的代码落后了几个版本。

我今天尝试在ngbuild 将我的文件放入指定文件夹后运行我的 django 本地服务器,

运行python manage.py collectstatic

它运行成功。

然后我运行我的 django 服务器,导航到我的页面,我得到一个 500 响应。

因为我使用的是 Angular,所以我将我的 django 服务器设置为 rest 后端。

rest 服务使用的每个端点都以 url api/ 开头

so localdomain/api/

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 和呈现 index.html 页面的 url

url(r'^(?!/?static/)(?!/?media/)(?P<path>.*\..*)$',
        RedirectView.as_view(url='/static/%(path)s', permanent=False)),
 url(r'^$', views.RootView.as_view()),

我的 rootview 类来渲染 index.html

class RootView(TemplateView):
    def get(self, request, **kwargs):
        return render(request, 'index.html', context = None)

【问题讨论】:

    标签: python django angular heroku whitenoise


    【解决方案1】:

    这是白噪声的常见问题。我为此浪费了无数个小时。以下是您如何处理此问题。

    将这两行放在你的 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 中删除引用该文件的行。

    在此之后,如果其他文件有问题,请继续这样做。

    最终白噪声会很高兴,你也会很高兴。

    【讨论】:

    • 谢谢你,我会检查一下,如果它有效,给你粘糊糊的分数!欣赏你!!
    • 但需要明确的是,我的 heroku 应用程序并没有因为显示 500 问题而中断,而是没有显示网站的新版本。你的修复仍然适用吗?
    • 500 是内部服务器错误,如果它显示其中断。你不打破什么最小?
    • 500 内部服务器错误仅显示在我的 django 本地上,而没有显示在 heroku 上。或者也许heroku只是绕过它并使用旧版本?没关系,我想我想多了
    • 好吧,如果它因为白噪声而中断,你有一个解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2017-05-06
    • 2013-01-26
    • 1970-01-01
    • 2018-12-27
    • 2016-12-18
    • 2020-12-28
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多