JSRB
JSRB

Reputation: 2613

How to make Django find a static css file

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

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)

Upvotes: 0

Views: 685

Answers (3)

vmiroslav
vmiroslav

Reputation: 1

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

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

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'), )

Upvotes: 3

Sammy J
Sammy J

Reputation: 1066

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

Upvotes: 0

Related Questions