Django staticfiles not found on Heroku (with whitenoise)

后端 未结 8 1397
我在风中等你
我在风中等你 2020-12-01 18:29

This question seems to be asked several time but I can not fix it.

I deployed a django app on production with DEBUG = False. I set my allowed_hos

相关标签:
8条回答
  • 2020-12-01 19:10

    With DEBUG=False, what originals use to work does not work for me anymore.

    However a fix by enabling whitenoise on MIDDLEWARE in settings.py solved it. Best to be just below SecurityMiddleware.

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'whitenoise.middleware.WhiteNoiseMiddleware', # add this line
        #Other middleware...
    ]
    

    ```

    According to the docs, it actually needs to be enabled in the first place.

    0 讨论(0)
  • 2020-12-01 19:10

    for BASE_DIR you need to a double dirname if your settings are not in the root but in /projectname/ folder :

    settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
    # for /static/root/favicon.ico    
    WHITENOISE_ROOT = os.path.join(BASE_DIR, 'staticfiles', 'root') 
    

    template.html

    {%  load staticfiles %}
    <link rel="stylesheet" href="{%  static "app/css/font.css" %}">
    

    app tree for this example:

    annuaire
    |-- /annuaire
    |-- -- /settings.py
    |-- /app
    |-- /static/app/css/font.css
    
    0 讨论(0)
提交回复
热议问题