0

I am going nuts with muy current Django project and its static files. I tried different solutions on SO
(e.g. Django cannot find my static files and Django Static Files CSS) and even my very own working ones from my other projects..

I just want to link a basic css file located in my projects /static/ folder to my base.html file which will contain the basic navbar for all sites/apps within the project. That's why I decided to place it in the projects directory centrally. Somehow it won't find the file though.

This is my setup where

  • debug is set to True (development, no production yet)

settings.py:

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

base.html:

{% load static %}
[...]
            {% block head_css_site %}
              <link href="{% static 'base.css' %}" rel="stylesheet" type="text/css">
            {% endblock head_css_site %}
[...]

project structure:

enter image description here

error:

GET http://127.0.0.1:8000/static/base.css net::ERR_ABORTED 404 (Not Found)
JSRB
  • 2,492
  • 1
  • 17
  • 48

3 Answers3

3

The problem is in your templates. As you can see, the request that is being made does not include the static prefix, which indicates that STATIC_URL is not defined. And, in fact, the {% load static %} you give at the top of the template does not (and cannot) define that variable. What it does do is give you access to the static template tag, which you use like this:

<link href="{% static 'base.css' %}"...>

Edit

Additionally, your static folder appears to be within your "dashex" directory, rather than the base dir. So you should either move it, or change the setting:

STATICFILES_DIRS = (os.path.join('dashex/static'), )
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you for your answer Mr. Roseman. I adapted your solution and the requested URL looks fine now. However, it still won't find the file? New error: `GET http://127.0.0.1:8000/dashex/static/base.css net::ERR_ABORTED 404 (Not Found)`. But going from the main dashex project folder it should be correct with `/dashex/static/base.css` where the manage.py file runs the development server. – JSRB Nov 03 '19 at 15:39
  • No, with the code you've given, the URL using this tag would be `http://127.0.0.1:8000/static/base.css`. Where did `dashex` come from? – Daniel Roseman Nov 03 '19 at 15:44
  • ah i tried to implement another solution given below and changed the static_url to `dashex/static/`. Changed it back again and I end up with `http://127.0.0.1:8000/static/base.css` now, but still `error not found`. Now I am totally confused. Why do both urls `http://127.0.0.1:8000/static/base.css` and `http://127.0.0.1:8000/dashex/static/base.css` end up with `ERR_ABORTED 404 (Not Found)` ? – JSRB Nov 03 '19 at 15:48
  • I moved the `templates` and `static` folders to the very main directory. Now it works. – JSRB Nov 03 '19 at 16:01
0

You can execute this :

    python manage.py collectstatic 

in the project, root to force it to collect static files

or set debug=True or run :

./manate.py runserver --insecure
vmiroslav
  • 1
  • 1
0

You must try giving the path to static files in the urls.py like this:

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)

check out the docs here Serving static files during development

Sammy J
  • 1,048
  • 10
  • 28
  • Added it, but still the same error. The GET is the issue I guess, not sure how to solve it though. – JSRB Nov 03 '19 at 15:33