简体   繁体   English

Django无法加载静态文件

[英]Django cant load static files

I have a webpage in Django and now I want to add some CSS (twitter bootstrap) to it. 我在Django有一个网页,现在我想添加一些CSS(twitter bootstrap)。 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. 我仔细阅读了文档 ,并为django开发服务器做了一切工作。 I am using development server with debug=True and django version 1.6.5. 我使用的开发服务器与debug = True和django版本1.6.5。 My settings.py looks like this: 我的settings.py看起来像这样:

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: 我的文件在/mysite/static/bootstrap/css文件夹下,在我的template.html中我有这个:

{% 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: 我甚至尝试在没有STATIC_ROOT的情况下进行设置,这样做:

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 您的静态文件位于boo t表带中

But, in your html, your wrote: {% static " boostrap /css/bootstrap.css" %} 但是,在你的html中,你写道:{%static“ boostrap /css/bootstrap.css”%}

There is a T missing. 有一个T失踪。

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. 您必须将STATIC文件加载到项目应用程序文件夹中。

---> If your project Name is myblog . --->如果您的项目名称是myblog You will find another folder named myblog into myblog and you have to place your static folder there. 你会发现一个名为其他文件夹myblogmyblog ,你必须把你static存在的文件夹。

---> In setting.py you have to add: --->在setting.py中你必须添加:

  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. 在生产模式下,如果收集静态,静态文件将被复制到root static中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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