简体   繁体   English

CSS不会从Django中的静态文件夹加载HTTP状态代码200

[英]CSS does not load from static folder in Django with HTTP status code 200

I know this has been asked hundreds of times over the years here , here , here (and others) but I can't load my css files from my static folder. 我知道这里已经有好几次这里这里这里 (以及其他人)被问了几次但是我无法从我的静态文件夹中加载我的css文件。

  • Django 1.7 Django 1.7
  • Python 2.7 Python 2.7

My command line says 我的命令行说

[07/Apr/2016 10:56:13] "GET / HTTP/1.1" 200 3596
[07/Apr/2016 10:56:13] "GET /static/compile/css/fbparse.min.6d40ca4cc832.css HTTP/1.1" 200 64
[07/Apr/2016 10:56:13] "GET /static/compile/js/fbparse.min.b40ef3f82f3a.js HTTP/1.1" 200 64

However, my log file says 但是,我的日志文件说

2016-04-07 10:45:14,333 [WARNING] django.request: Not Found: /static/compile/css/fbparse.min.6d40ca4cc832.css
2016-04-07 10:56:13,848 [WARNING] django.request: Not Found: /static/compile/css/fbparse.min.6d40ca4cc832.css
2016-04-07 10:56:13,852 [WARNING] django.request: Not Found: /static/compile/js/fbparse.min.b40ef3f82f3a.js

And of course, the site does not load the css file. 当然,该网站不会加载css文件。

When I try 当我尝试

http://127.0.0.1:8000/static/compile/css/fbparse.min.6d40ca4cc832.css

It gives me error 404 它给了我404错误

Here is what I have in settings file 这是我在设置文件中的内容

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
rel = lambda *x: os.path.join(BASE_DIR, *x)

DEBUG = False

MEDIA_ROOT = rel('public', 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = rel('public', 'static')
STATIC_URL = '/static/'


STATICFILES_DIRS = (
    rel('static_src'),
)

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

.....
)

Here is what I have in the Base html 这是我在Base html中的内容

<!DOCTYPE html>
{% load pipeline static i18n stats_tags %}
<html>
<head>
    <meta charset=utf-8 />

    <title>My site title</title>

    {% stylesheet 'global' %}

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    {% javascript 'global' %}
    {% if debug %}
        <script src="{% static "components/less.js/dist/less.min.js" %}"></script>
        {% if LESS_DEBUG %}
            <script type="text/javascript">
                less.env = "development";
                less.watch();
            </script>
        {% endif %}
    {% endif %}

</head>

I have tried putting the static folder in the root directory, inside public, inside static_src but nothing seems to work. 我已经尝试将静态文件夹放在static_src中的公共内部的根目录中,但似乎没有任何效果。

Can anyone tell me what I am doing wrong? 谁能告诉我我做错了什么?

Did you use manage.py runserver ? 你使用manage.py runserver吗?

In that case you want to use dev-server with DEBUG = False , you can simulate it via manage.py runserver --insecure . 在这种情况下,您希望使用DEBUG = False dev-server,您可以通过manage.py runserver --insecure进行模拟。

If you want to deploy your django project with WAS like Apache, you need to serve your static files independently as stated in Docs . 如果您想使用像Apache这样的WAS部署django项目,则需要按照文档中的说明独立提供静态文件。

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

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