简体   繁体   中英

Django static files (css) not working

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.html and 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),
]

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.

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!

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.

what if your static link starts with just appname ?

ie, 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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM