【问题标题】:Bootstrap is not hit to HTMLBootstrap 未命中 HTML
【发布时间】:2024-05-23 15:50:02
【问题描述】:

引导程序未命中 HTML。 我使用 Flat UI 的 Bootstrap。 我写在 index.html 之类的

{% load staticfiles %}
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{% static './bootflat.github.io/bootflat/css/bootflat.min.css' %}">

    <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>
    <style>
      body {
        padding-top: 70px;
      }

      .my-form {
        width: 640px;
        margin: 0 auto;
      }

      @media screen and (max-width: 768px) {
        .my-form {
          width: 100%;
        }
      }

      .errorlist li {
        list-style-type: none;
      }

      .errorlist {
        color: red;
        margin-left: 0;
        padding-left: 0;
      }
    </style>
  </head>
  <body>
    <nav class="navbar navbar-default" role="navigation">
        <div class="navbar-header">
            <p class="navbar-text">Hello</p>
            {% if user.is_authenticated %}
            <p class="navbar-text">{{  user.get_username }}</p>
            {% endif %}
        </div>
    </nav>

    <div class="container">
      {% block content %}
      {% endblock %}
    </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>

我指定了像&lt;link rel="stylesheet" href="{% static './bootflat.github.io/bootflat/css/bootflat.min.css' %}"&gt;这样的Flat UI的文件夹路径,但设计没有改变。我访问了http://localhost:8000/static/bootflat.github.io/bootflat/css/bootflat.min.css,but找不到页面(404)请求方法:GET请求URL错误,所以我认为路径错误。 目录结构是

index.html 在accounts.accounts 文件夹结构是

我的代码有什么问题?我应该如何解决这个问题?

【问题讨论】:

  • 先跟着教程学习一下django项目的结构。

标签: python html django bootstrap-4


【解决方案1】:

我认为您错过了在 settings.py 中包含 STATICFILES_DIRS。 将此行添加到您的 settings.py。

    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'bootflat.github.io'), ]

然后通过以下方式访问模板中的静态文件:

     {% static 'bootflat/css/bootflat.min.css' %}

STATICFILES_DIRS 用于指定查找静态文件的目录。

在此之前

您需要了解 django 项目结构的工作原理。为此,请遵循一些教程。

【讨论】:

  • 我第一次投反对票只是为了说,你应该鼓励他完成 django-project staructure 的工作。但是现在在我编辑了你的答案后,我投票赞成。希望你不要介意。