-1

I know this question was previously asked and I already tried many different configurations to fix this. I had no problem serving my static files locally when developing but, if I remember correctly (don't know exactly because of the browser cache), I ran python manage.py collectstatic and then all static files were nowhere to be found:

[15/May/2023 08:55:59] "GET /static/js/script.js HTTP/1.1" 404 179
[15/May/2023 08:55:59] "GET /static/admin/css/responsive.css HTTP/1.1" 404 179
...

Here is my settings.py:

DEBUG = ENV.bool("DEBUG")  # set to True

INSTALLED_APPS = [
    "django.contrib.staticfiles",
    ...
]

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / STATIC_PATH  # I have checked that the path is correct
STATICFILES_DIRS = [  # I tried with and without STATICFILES_DIRS
    BASE_DIR / "vhsapp" / "static"
]

urls.py

...

if settings.DEBUG:
    urlpatterns += [
        static(settings.STATIC_URL, document_root=settings.STATIC_ROOT),
    ]

base_site.html

{% extends "admin/base_site.html" %}

{# TEMPLATE EXTENSION FOR THE DEFAULT ADMIN INTERFACE #}
{% load static %}

{% block extrahead %}
    {{ block.super }}
    <link rel="icon" type="image/x-icon" href="{% static 'img/favicon.png' %}"> <!-- 404 error -->
    <script>
        const APP_NAME = "{{ APP_NAME }}"; // variable defined correctly
    </script>
{% endblock %}

Project arborescence

project_root/
├── staticfiles/           # all static files are inside this directory
├── templates/
│   └── admin/
│       └── base_site.html
├── vhs/
│   ├── .env
│   ├── urls.py
│   └── settings.py
├── vhsapp/
│   ├── static/             # additional static files (collectstatic put them inside staticfiles/)
│   ├── templates/          # additional templates
│   └── __init__.py
└── manage.py

I have read the docs but I really cannot see what might have happened. I know the project arborescence is a little weird but everything used to work!

Do you have any idea what i am missing?

Thank you X1000

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Seglinglin
  • 447
  • 1
  • 4
  • 17
  • Config looks fine, `STATICFILES_DIRS` in your case is redundant. Show `collectstatic` command output. – Ivan Starostin May 15 '23 at 10:32
  • `0 static files copied to 'project_root/staticfiles', 9739 unmodified.` (previously 9739 copied) I tried changing to `static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True),` but nothing has changed – Seglinglin May 15 '23 at 11:05
  • 1
    [RESOLVED] this is silly, i had a comment in my .env file, and the DEBUG variable was not parsed correctly! – Seglinglin May 15 '23 at 11:21

1 Answers1

0

in production you should handle static file by webservers like nginx and you can you some packages like white noise it can serve you static file without need to any webservers some config like this

location /static/{
    autoindex on;
    alias /home/app/django-app/staticfiles/; // => the path to the static folder
}

in your nginx config