简体   繁体   中英

Django cant load static files

I have a webpage in Django and now I want to add some CSS (twitter bootstrap) to it. This is the first I am trying. I have carefully read the docs and did everything said there for the django development server to work. I am using development server with debug=True and django version 1.6.5. My settings.py looks like this:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
 )

My files are under /mysite/static/bootstrap/css folder and in my template.html I have this:

{% load staticfiles %}
<link href="{% static "boostrap/css/bootstrap.css" %}" rel="stylesheet" media="screen">

Unfortunately, nothing happens, I see that the development server says:

 "GET /static/boostrap/css/bootstrap.css HTTP/1.1" 500 59

which means it cannot find them. I even tried doing the settings without STATIC_ROOT, doing this:

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

but then the development server returns:

"GET /static/boostrap/css/bootstrap.css HTTP/1.1" 404 1682

Any help appreciated.

You did everything right, the issue here is a simple typo.

Your static files are located in boo t strap

But, in your html, your wrote: {% static " boostrap /css/bootstrap.css" %}

There is a T missing.

The code below will work:

{% load staticfiles %}
<link href="{% static "bootstrap/css/bootstrap.css" %}" rel="stylesheet" media="screen">

我发现这个问题很多次,每次问题都通过将URL添加到STATIC_URL来解决

STATIC_URL = 'http://localhost:8000/static/'

You have to load the STATIC file into your project app folder.

---> If your project Name is myblog . You will find another folder named myblog into myblog and you have to place your static folder there.

---> In setting.py you have to add:

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

In your production mode if you collect static , static file will be copied in root static.

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