2

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),
]
3
  • Remove static in 'static/appname/css/bootstrap.min.css' (or static/appname, i'm not 100% sure) Commented Mar 9, 2016 at 15:20
  • And maybe if you are not in debug mode, you have to run collectstatic from manage.py Commented Mar 9, 2016 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... Commented Mar 9, 2016 at 15:22

6 Answers 6

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

I am using my local lightweight server that came with Django, just to get the feel of things :). Spasiba bolshoe!
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.

1 Comment

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. "
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.

1 Comment

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 ...
-1
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

1 Comment

You can improve your answer by adding some explanatory text.
-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/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.