DJango not finding static files, despite STATIC_URL and STATIC_ROOT set

Viewed 1983

I have just started learning DJango and am trying to use css file for my html template. As per the documentation, I have added the STATIC_URL and STATIC_ROOT to my settings.py file as follows:

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

BASE_DIR is already set as follows:

BASE_DIR = Path(__file__).resolve().parent.parent

In the template, I am using the css file as follows

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

and the style.css is present at location DJango_App/static, DJango_App being my project directory with the manage.py file. Still I am getting error

"GET /static/style.css HTTP/1.1" 404 1653

DEBUG is set to True

Directory structure is:

DJango_App
|->DJango_App
   |->(settings.py, urls.py, views.py, etc)
|->templates
   |->(html templates)
|->static
   |->style.css

How do I resolve this?

1 Answers

Update:

Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS, see:

https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS

Original:

Looks like you did not add the static url handler to urlpatterns.

Serving static files during development require add to urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Related