1

I can't seem to include my bootstrap css files for some reason. I am quite new to Python and Django especially, so I am definitely doing something wrong.

  • Django 1.9.2

After reading the official Django explanation on the "Static files" management I am absolutely zero smarter :(. Here is my project folders hierarchy:

/projectname/
    /appname/
        /static/
        |   /appname/
        |        /css/
        |        |    bootstrap.min.css
        |        |    custom.css
        |        /img/
        |        /js/
        |
        /templates/
            /includes/
                head.html
                footer.html
            index.html
            base.html

I started with the basics so I disregarded the head.htmland tried with the base.html like so:

<title>{% block title %}{% endblock %}</title>

<!-- Bootstrap core CSS -->
{% load staticfiles %}
<link href="{% static 'static/appname/css/bootstrap.min.css' %}" rel="stylesheet">

No luck. Here is my settings file:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
...

STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILE_DIRS ='/users/edchigliak/documents/projects/projectname/appname/static/'

As fas as I understand, it is possible to have a "global" 'static files location' which all your projects can use, and "per app" 'static files location' which can be uses only by the app inside which base directory they reside.

Any help appreciated!

EDIT:

This is my urls.py configuration:

from django.conf.urls import url
from django.contrib import admin
from budgeteer.views import hello, hours_ahead, current_datetime

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', hello),
url(r'^index/$', current_datetime),
url(r'^time/plus/(\d{1,2})/$', hours_ahead),
]
Alexander Starbuck
  • 1,139
  • 4
  • 18
  • 31
  • Remove static in 'static/appname/css/bootstrap.min.css' (or static/appname, i'm not 100% sure) – Benjamin Mar 09 '16 at 15:20
  • And maybe if you are not in debug mode, you have to run collectstatic from manage.py – Benjamin Mar 09 '16 at 15:21
  • Ah, you see, I read about the `collectstatic` but I hate doing something that I do not understand. Is it so that this command takes the files from my defined locations and puts them somewhere else (copies) and THEN includes them in the template? This is a bit confusing... – Alexander Starbuck Mar 09 '16 at 15:22

6 Answers6

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
  • 1,346
  • 1
  • 14
  • 16
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
  • 4,405
  • 2
  • 34
  • 54
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
  • 6,837
  • 6
  • 32
  • 52
  • I think it's normal to have static dir in app From django doc : "Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. " – Benjamin Mar 09 '16 at 15:27
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
  • 894
  • 8
  • 21
  • Nope, unfortunately still not working. Here is what I get from terminal when I `runserver : [10/Mar/2016 08:14:37] "GET /index/ HTTP/1.1" 200 4872 Not Found: /index/js/ie-emulation-modes-warning.js Not Found: **/index/**css/ie10-viewport-bug-workaround.css Not Found: /index/css/jumbotron.css ... Not Found: /dist/js/bootstrap.min.js Not Found: /assets/js/ie10-viewport-bug-workaround.js ...` – Alexander Starbuck Mar 10 '16 at 08:17
-1
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-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/.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135