[Answered ]-Django static files (css) not working

3๐Ÿ‘

I have similar problem (Django 1.10). Hierarchy:

myapp
   ...
   myapp
      ...
   blog
      migrations
      templates
      ...
   static
      blog
         style.css

So if I add <link href="{% static 'blog/css/bootstrap.min.css' %}" rel="stylesheet"> (style.css located in dir โ€˜blog/cssโ€™) all styles wonโ€™t work.

BUT when I delete โ€˜cssโ€™: <link href="{% static 'blog/bootstrap.min.css' %}" rel="stylesheet"> (style.css located in dir โ€˜blogโ€™) itโ€™s ok.

May be it help you!

๐Ÿ‘คJackssn

2๐Ÿ‘

I think you need to add following to your URLs:

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)

unless you work on Django server and it serves your static files.

According you the Django docs your app structure is OK.

When you will setup your prod and start serve static by Apache/Nginx/etc, than you will need to run collectstatic.
For now it donโ€™t needed.

๐Ÿ‘คAndrii Rusanov

0๐Ÿ‘

My quick guess is that you are one level up. You have your static directory nested under appname. Try moving it up a level and accessing the resource directly in the browser (http://example.com/static/appname/css/bootstrap.min.css)

Iโ€™ve never done app specific resources, so if that is the goal, my apologies.

๐Ÿ‘คAdam Hopkins

0๐Ÿ‘

what if your static link starts with just appname?

i.e., instead of

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

please try

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

AFAIK, the string in {% static %} is the path to a static file inside the static folder.

I donโ€™t have points enough to comment, so I leave my guess here.

๐Ÿ‘คLeonard2

-1๐Ÿ‘

STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
๐Ÿ‘คhasan siam

-2๐Ÿ‘

You need to put this line on the outside of HTML tags.

{% load static %}

I found the answer here: https://tutorial.djangogirls.org/en/css/.

๐Ÿ‘คSasha Muller

Leave a comment