【问题标题】:Bootstrap static files not being included in Django template引导静态文件未包含在 Django 模板中
【发布时间】:2016-02-08 19:20:16
【问题描述】:

我是 Django 新手,在 django 页面上呈现引导程序时遇到问题。

这是我的基本 html,

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Dropbox Web App Prototype</title>

    <!-- Bootstrap -->
    <link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">

    <!-- 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]-->
  </head>
  <body>
   <div class="jumbotron">
       <h1>Hello, world!</h1>
        <p>This is a template showcasing the optional theme stylesheet included in Bootstrap. Use it as a starting point to create something more unique by building on or modifying it.</p>
      </div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
  </body>
</html>

静态文件与项目目录同级,结构如下,

我还在我的 settings.py 文件中添加了以下行

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

CSS 未在主页上呈现。任何帮助表示赞赏。

【问题讨论】:

  • 你使用哪个 Django 版本?
  • 静态文件应在您的项目文件夹下(即与 urls.py 和 settings.py 位于同一文件夹),并确保您的应用已添加到您的 settings.py INSTALLED_APPS 列表中
  • @pp_ 1.9.2 是版本
  • @JoranBeasley 它就在那里,我已将应用添加到已安装应用列表中。

标签: python css django twitter-bootstrap


【解决方案1】:

BASE_DIR 默认定义为:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

这里__file__实际上是settings.py,所以BASE_DIR在父目录上,包含manage.py

您的static 文件夹似乎位于另一个级别,因此只需将其移动到与manage.py 相同的级别即可。

【讨论】:

    【解决方案2】:

    CDN:

    <link rel="stylesheet" type="text/css"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"  crossorigin="anonymous">
    

    静态

    在 django runserver 的 urls 中:

    if settings.DEBUG:
        urlpatterns += [url(r'^static/(?P<path>.*)$', views.serve),]
    

    从网络服务器运行时,您需要配置别名。 Nginx 示例:

    location  /static { 
        alias /home/username/mysite/static;}
    

    settings.py:

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

    【讨论】:

    • 127.0.0.1:8000/static 还是 127.0.0.1/static?顺便说一句,对于引导 CDN 是可以的
    【解决方案3】:

    只需在您的设置中定义 additioaly 静态 url,如下所示:

    STATIC_URL = '/static/'

    并再次检查您的引导程序是否已提供服务

    【讨论】:

      【解决方案4】:

      你能粘贴你的 BASE_DIR 吗?可以设置成:BASE_DIR to BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

      【讨论】:

      • BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
      • 从一开始就是这样。
      • 这只是一个猜测,但是如何将 'vendor' 添加到 所以它看起来像
      【解决方案5】:

      在html中 替换这个

      <link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
      

      有了这个

      <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
      

      并检查这是否在设​​置中。

      STATICFILES_DIRS = [
          os.path.join(BASE_DIR, "static"),
      ]
      

      【讨论】:

      • 静态标签 引用静态文件的首选方式,将其更改为使用相对 url 不会改变任何内容
      • true 但看起来他的 bootstrap 文件夹位于名为 static 的文件夹中,因此他需要在他的 href 和我的作品中引用它
      • 这由相关的STATICFILES_DIRS 设置处理(现在使用数组而不是元组(尽管元组仍然可以工作))
      • 很好,我的设置是这样的,并且与引导程序完美配合。他没有给我们一大堆继续下去的机会。如果它修复它,它会修复它。需要注意的是,我不是唯一一个在他们的答案中使用元组的人。
      • SpeedyH30,最初我的反对意见是针对您答案的第一部分,这没有任何效果,经过反思,我认为这有点苛刻,但是此时已经过去了 5 分钟,这意味着唯一的我可以删除我的 DV 的方法是如果帖子被编辑,因为 django 1.9 使用数组而不是元组,我用它作为替罪羊编辑来删除它。道歉!
      【解决方案6】:

      您使用的旧语法可能与您的 Django 版本不匹配。 在您的 html 文件更改中:

      {% load staticfiles %}
      

      {% load static %}
      

      另外,用双引号替换单引号:

      "{% static 'bootstrap/css/bootstrap.min.css' %}"
      

      到:

      "{% static "bootstrap/css/bootstrap.min.css" %}"
      

      你可以看到处理静态文件的确切语法here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-29
        • 2016-04-13
        • 1970-01-01
        • 2012-12-10
        • 2012-06-03
        • 1970-01-01
        • 2012-02-28
        • 2013-04-12
        相关资源
        最近更新 更多