Search code examples
pythondjangodjango-staticfiles

Django doesn't recognize my static folder


I just started my django project and i saw that i had to put my static folder in my project folder so i did, i think i did the necessary modifications to settings.py but when a ran my server, the css wasn't applying to my page, i looked at where my css file was looked for and it said "404 /store/static/css/carousel.css" ("store" is my app inside my project) while it should look for "myproject/static/css/carousel.css".

So I moved my static folder inside my app folder ("store") at the location the server was looking for (store/static/css/carousel.css) but even if it is now looking at the right location it is still not working.

settings.py

BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = 'static/'
STATICFILES_DIR = [os.path.join(BASE_DIR, '/static/')]

html page referencing my css file

{% extends 'store/base.html' %}
{% load static %}

{% block head %}
<link href="{% static 'css/carousel.css' %}" rel="stylesheet" type="text/css">
{% endblock %}

server response

Not Found: /store/static/css/carousel.css
[07/Aug/2023 10:00:27] "GET /store/static/css/carousel.css HTTP/1.1" 404 2378

i have these directories

.
├── myproject
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
└── store
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── static
    │   ├── css
    │   │   ├── carousel.css
    │   │   ├── main.css
    │   │   └── pricing.css
    │   ├── js
    │   └── media
    ├── templates
    │   └── store
    │       ├── base.html
    │       ├── form.html
    │       ├── merch.html
    │       ├── renting.html
    │       └── selling.html
    ├── tests.py
    ├── urls.py
    └── views.py

Solution

  • You may change the settings.py like this:

    # Static files config
    
    STATIC_URL = '/static/'
    STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]
    

    And remove the static folder from inside the store folder and place it next to other apps.

    And remove the type attribute in the link tag from the html file.

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