简体   繁体   English

为什么我的 static 文件在部署到 Heroku 服务器时无法提供服务? (姜戈)

[英]Why are my static files not served when deployed to Heroku server? (Django)

So this is a common mistake for Django users, which is static files not served in production when Debug=False .所以这是 Django 用户的常见错误,即当Debug=False时 static 文件未在生产中提供。 I've tried many methods to overcome this issue, but still cannot figure out right solution.我已经尝试了很多方法来克服这个问题,但仍然无法找到正确的解决方案。 Below is my settings.py下面是我的settings.py

...
DEBUG = False

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]
...

STATIC_URL = '/static/'
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'static'), 
    os.path.join(BASE_DIR, 'main/static'),
    os.path.join(BASE_DIR, 'member/static'),
    os.path.join(BASE_DIR, 'register/static'),
]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

I don't see how I've done wrong.我不明白我怎么做错了。

*One thing that might be a potential cause is that I've set DISABLE_COLLECTSTATIC=1 since the initial deploy to heroku server. *可能是潜在原因的一件事是,自从最初部署到 heroku 服务器以来,我已经设置了DISABLE_COLLECTSTATIC=1

remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/python
remote: -----> Python app detected
remote: -----> Using Python version specified in runtime.txt
remote: -----> No change in requirements detected, installing from cache
remote: -----> Using cached install of python-3.7.12
remote: -----> Installing pip 21.3.1, setuptools 57.5.0 and wheel 0.37.0
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Skipping Django collectstatic since the env var DISABLE_COLLECTSTATIC is set.
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 102.1M
remote: -----> Launching...
remote:        Released v153

Try this:尝试这个:

  1. Add to your setting.py installed apps 'whitenoise.runserver_nostatic' .添加到您的 setting.py 安装的应用程序'whitenoise.runserver_nostatic'
  2. Add to your setting.py MIDDLEWARE 'whitenoise.middleware.WhiteNoiseMiddleware'添加到您的 setting.py MIDDLEWARE 'whitenoise.middleware.WhiteNoiseMiddleware'
  3. Add to your seeting.py these 3 lines添加到您的 seeting.py 这 3 行
STATIC_URL = '/static/'
STATICFILES_DIRS = (str(BASE_DIR.joinpath('static')),)
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # new
django_heroku.settings(locals())

for the last line here you need to install django_heroku (not django_on_heroku , be careful)对于这里的最后一行,您需要安装django_heroku (不是django_on_heroku ,要小心)

For more info check this out: https://learndjango.com/tutorials/django-static-files .有关更多信息,请查看: https://learndjango.com/tutorials/django-static-files

PS if after this it fails because it can't find the staticfiles folder, manually create it. PS如果在此之后它因为找不到staticfiles文件夹而失败,请手动创建它。 You have to do it only once and that's it.你只需要做一次就可以了。 An empty folder and nothing more.一个空文件夹,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM