导致Django500错误的静态文件问题?

2024-04-19 04:34:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

正在运行python manage.py collectstatic

它运行成功。在

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

因为我使用angular,所以我将django服务器设置为rest后端。在

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

so 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),

]

我的模板设置:

^{pr2}$

提供静态文件的url和呈现索引.html页码

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

我的rootview类来呈现索引.html在

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

Tags: 文件pathdjango服务器urlbaseherokuos
1条回答
网友
1楼 · 发布于 2024-04-19 04:34:16

这是白噪声的常见问题。我为此浪费了无数的时间。下面是你如何处理这个问题。在

把这两条线放到你的设置.py在

DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = True

尝试在heroku上运行应用程序。它会像往常一样破裂。在

现在转到

^{pr2}$

通常,当debug为False时,您将无法看到日志,但由于debug\u PROPAGATE_EXCEPTIONS=True您可以看到日志

在那里,寻找白噪音找不到的文件,让白噪音失控。在我的例子中,它是一些随机的css文件,在my基本.html。在

您可以固定其位置,也可以简单地从中删除行基本.html引用此文件。在

在此之后,如果tehre与其他文件有问题,请继续执行。在

最终白噪音会快乐,你也会。在

相关问题 更多 >