You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Django应用至Heroku时遇STATICFILES_DIRS配置错误求助

Fix: 'The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting' in Django on Heroku

Hey there! Let's break down this error and fix it step by step—this is a super common gotcha when setting up static files for Django deployments, especially on Heroku.

What's causing this error?

Django uses two key settings for static files, and mixing them up triggers this message:

  • STATIC_ROOT: This is the directory where the collectstatic command dumps all your static files (from apps and other sources) into a single location—Heroku needs this to serve static files efficiently.
  • STATICFILES_DIRS: This tells Django where to look for extra static files that aren't stored inside individual app static/ folders (like a project-wide static/ directory).

The error happens because you've accidentally added STATIC_ROOT to STATICFILES_DIRS. This creates a conflict: Django would try to read files from the same directory it's supposed to write them to during collectstatic.

How to fix it

  1. Check your settings.py for conflicting config
    First, look for lines like this (this is the problematic setup):

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
        os.path.join(BASE_DIR, 'staticfiles'),  # ❌ This is the culprit!
    ]
    
  2. Remove STATIC_ROOT from STATICFILES_DIRS
    Update your config to separate the two directories properly. Here's a correct example:

    import os
    from pathlib import Path
    
    BASE_DIR = Path(__file__).resolve().parent.parent
    
    STATIC_URL = '/static/'
    
    # Target directory for collectstatic (auto-generated, don't add to STATICFILES_DIRS)
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    
    # Only include your manually created static directories here
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]
    
    # Use WhiteNoise for serving static files on Heroku (required for proper deployment)
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    
  3. Verify locally before deploying
    Run the collectstatic command on your machine to make sure the config works:

    python manage.py collectstatic
    

    If this runs without errors, you're good to go.

  4. Double-check Heroku setup (if needed)

    • Make sure whitenoise is in your requirements.txt (add whitenoise==6.6.0 or a compatible version).
    • Update your wsgi.py to include the WhiteNoise middleware:
      from django.core.wsgi import get_wsgi_application
      from whitenoise import WhiteNoise
      import os
      from pathlib import Path
      
      BASE_DIR = Path(__file__).resolve().parent.parent
      os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings')
      
      application = get_wsgi_application()
      application = WhiteNoise(application, root=os.path.join(BASE_DIR, 'staticfiles'))
      

Quick recap

The core issue is a simple configuration mix-up: STATIC_ROOT is a destination directory, not a source. Remove it from STATICFILES_DIRS, ensure your static sources are only in STATICFILES_DIRS, and your deployment should work smoothly.

内容的提问来源于stack exchange,提问作者Anuj TBE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:24:38