【问题标题】:Railway.app can't find staticfiles after deployment django app部署django app后Railway.app找不到静态文件
【发布时间】:2022-11-01 16:30:12
【问题描述】:

我尝试将我的应用程序部署到 Railway.app。应用程序通常可以工作,但找不到静态文件。 我按照部署教程中的建议使用命令django manage.py collectstatic 创建了collectstatic 文件夹,但它没有帮助。 任何线索可能出了什么问题?

设置.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

staticfiles 文件夹位于包含 menage.py 文件的目录中

从 rail.apps 部署日志

Not Found: /staticfiles/style.css
Not Found: /staticfiles/base.css
Not Found: /staticfiles/MyImage.png
Not Found: /staticfiles/base.js

【问题讨论】:

    标签: django django-staticfiles


    【解决方案1】:

    WhiteNoise 为我工作:

    pip install whitenoise
    

    在你的 settigs.py 中:

    INSTALLED_APPS = [
        'django.contrib.staticfiles',
        'whitenoise.runserver_nostatic',    
    ]
    
    MIDDLEWARE = [
        "django.middleware.security.SecurityMiddleware",
        "whitenoise.middleware.WhiteNoiseMiddleware",
    ]
    
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    STATICFILES_STORAGE="whitenoise.storage.CompressedManifestStaticFilesStorage"
    

    【讨论】:

      【解决方案2】:

      我有类似的问题,我认为它与铁路应用程序有关,我最终使用 Cloudinary 来存储我的静态文件

      pip install django-cloudinary-storage
      

      完成此操作后,将 cloudinary 和 cloudinary_storage 添加到您在 settings.py 中安装的应用程序中。如果您打算将此包用于静态和/或媒体文件,请确保 cloudinary_storage 在 django.contrib.staticfiles 之前:

      INSTALLED_APPS = [
          # ...
          'cloudinary_storage',
          'django.contrib.staticfiles',
          'cloudinary',
          # ...
      ]
      

      接下来,您需要将 Cloudinary 凭据添加到 settings.py:

      CLOUDINARY_STORAGE = {
          'CLOUD_NAME': 'your_cloud_name',
          'API_KEY': 'your_api_key',
          'API_SECRET': 'your_api_secret'
      }
      

      然后运行

      python manage.py collectstatic --noinput
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,@Jorge20dml 的答案对我有用,但也许有人会更清楚地遵循this instruction 一步一步。

        【讨论】:

          猜你喜欢
          • 2016-03-11
          • 2021-09-12
          • 2020-09-28
          • 2016-04-16
          • 2013-09-23
          • 2021-08-27
          • 2014-11-10
          • 2011-08-26
          • 2020-06-22
          相关资源
          最近更新 更多