【问题标题】:Django not loading CSS?Django不加载CSS?
【发布时间】:2014-01-31 17:58:55
【问题描述】:

这是我的 urls.py

import os.path
site_media = os.path.join(
    os.path.dirname(__file__), 'site_media'
)

urlpatterns = patterns('',
    url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
    { 'document_root': site_media }),
)

我的 site_media 文件夹和 style.css 文件在

myProjectFolder/myApp/static/site_media/css/style.css

在我的 base.html 模板中(我所有的其他模板都扩展自这个 base.html 模板)这就是我所拥有的:

<head>
<title>{% block title %}{% endblock %}</title>
<link rel='stylesheet' type='text/css' href='/site_media/css/style.css' />
</head>

<body>
    {% block header %}{% endblock %}

    {% block content %}
        <div id='header'></div>
    {% endblock %}
</body>

在我的 style.css 中,我只有这个:

#header {
    display: inline;
    background-color: black;
    color: black;
    border: 1px solid green;
}

CSS 没有被应用到

#header

分区。知道为什么吗?

【问题讨论】:

    标签: html css django django-templates django-staticfiles


    【解决方案1】:

    问题来自两个方面。

    1. 传递了错误的 document_root 变量
    2. 不在 html 中使用模板标签

    1。 不鼓励在 urls.py 文件中定义 site_media 变量,因为它不符合 DRY 原则。它应该作为一个变量保存在您的 settings.py 文件中,以便在需要时可以导入其他模块。 在您的 settings.py 文件中,添加以下行:

    import os.path
    SITE_MEDIA_ROOT = os.path.join(
        os.path.dirname(__file__), 'myApp/', 'static/', 'site_media' #this should be the correct path instead
    )
    

    用这个替换你的 urls.py 文件:

    from myApp import settings
    
    urlpatterns = patterns('',
    url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
    { 'document_root': settings.SITE_MEDIA_ROOT }),
    )
    

    2.你应该使用django模板标签来获取css文件。在您的基本模板中,在 head 标签上方添加这一行:

    {% load static %}
    

    然后替换:

    <link rel='stylesheet' type='text/css' href='/site_media/css/style.css' />
    

    与:

    <link rel='stylesheet' type='text/css' href="{% static 'site_media/css/style.css' %}" />
    

    之后它应该可以工作了。

    【讨论】:

      【解决方案2】:

      两种选择:

      1. 您可以在 urls.py 中稍微修改您的代码

      在 urls.py 中

      site_media = os.path.join(
          os.path.dirname(__file__), 'site_media'
      )
      

      site_media = os.path.join(
          os.path.dirname(__file__), "../", "myApp", "static", 'site_media'
      )
      

      2。删除

      site_media = os.path.join(
          os.path.dirname(__file__), 'site_media'
      )
      

      并添加

      {% load static %} at the beginning of base.html
      

      改变

      <link rel='stylesheet' type='text/css' href='/site_media/css/style.css' />
      

      <link rel='stylesheet' type='text/css' href="{% static 'site_media/css/style.css' %}" />
      

      第二种方法更可取。 这是工作代码:https://github.com/lukaszszajkowski/s21083672

      以下是文档和阅读链接: https://docs.djangoproject.com/en/dev/howto/static-files/#configuring-static-files Media files are served, static files aren't

      【讨论】:

        猜你喜欢
        • 2020-07-06
        • 2018-02-03
        • 2020-01-26
        • 2013-07-01
        • 2020-09-26
        • 2019-01-16
        • 2020-08-07
        • 1970-01-01
        • 2020-08-23
        相关资源
        最近更新 更多