Search code examples
djangodjango-staticfiles

CSS files not loading in django


This is my first Django project. I cannot load CSS files onto my website. i've done everything possible.

Project Directries

firstproject/
  assets/
  static/
    css/
      <CSS files>

Settings.py/ static files

STATIC_URL = 'static/'
STATICFILE_DIRS = [
    os.path.join(BASE_DIR, "static")
]

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

urls.py

urlpatterns  += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

HTML file

<link rel="stylesheet" href="{% static 'css/bootstrap-grid.css' %}">

Solution

  • I think you made spelling mistake in staticfilesdirs in settings.py file.

    Change this:

    STATIC_URL = 'static/'
    STATICFILE_DIRS = [ #Here you made spelling mistake. It should be  `STATICFILES_DIRS`
        os.path.join(BASE_DIR, "static")
    ]
    

    To this:

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

    And in link tag just add type="text/css".