Error while loading static files in Django

  django, django-static, python, python-3.x

Trying to load static files but it is showing following error.

ERROR

GET http://127.0.0.1:8000/static/css/user/style.css net::ERR_ABORTED 404 (Not Found) - home:25
GET http://127.0.0.1:8000/static/css/user/style.css 404 (Not Found) - home:25
GET http://127.0.0.1:8000/static/img/logo.png 404 (Not Found) - home:149
GET http://127.0.0.1:8000/static/img/logo.png 404 (Not Found) - home:1

Click here to see error image.

CODE

-ADMIN

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")

urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Welcome.urls')),
    path('auth/', include('Authentication.urls')),
    path('ad/', include('Ads.urls')),
    path('user/', include('UserDashboard.urls')),
    path('admin/', include('AdminDashboard.urls')),
]

if settings.DEBUG:
    urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  • APPS

template

<link href="{% static 'css/user/style.css' %}" rel="stylesheet">

Dirs Structure

Click here to Project Directory Structure Image

CODE EXPLANATION

  • Simply added static files in the root dir and tried to import them in template but css files are not loading
  • But some media files are successfully loaded like this media <link rel="shortcut icon" type="image/jpg" href="{% static 'img/logo.png' %}" />.
  • Also in dir structure, we can see img and css folder at same place in static folder and images from image folder are loaded but css from css folder does not.

Source: Python Questions

LEAVE A COMMENT