【问题标题】:Why doesn't css styles apply to html correctly in django?为什么 css 样式不能在 django 中正确应用于 html?
【发布时间】:2022-07-18 18:52:26
【问题描述】:

在 django 中只有 body 样式可以工作,我使用静态文件来获取 css。我什至尝试注释掉body part css,但它仍然有效,我不知道如何,我不知道为什么,请帮助! (我用过 collectstatic,所以不是因为这个)。我在设置中插入了 static_root 和 url 和其他东西,但它也没有帮助。

body{
    max-width: 1080px;
    margin: auto;
    background: #8D7D77;
    font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}

/* ----------------------------------------------NAV-BAR-UPPER------------------------------------------ */

.nav-bar{
    display: flex;
    flex-direction: row;
    padding-top: 20px;
}

.nav-bar img{
    max-width: 150px;
    margin-left: 20px;
}

.nav-bar ul{
    list-style: none;
    display: flex;
    flex-direction: row;
    
}

.nav-bar ul li{
    margin-right: 15px;
}

.nav-bar ul a{
    text-decoration: none;
    color: white;
    font-size: 20px;
    margin-left: 80px;
    padding: 10px 20px 10px 20px;
    border: solid 1px;
    border-radius: 10px;
}

.nav-bar ul a:hover{
    text-decoration: none;
    color: #8D7D77;
    background-color: white;
}

.search-bar{
    width: 600px;
    height: 40px;
    margin-top: 10px;
    margin-left: 50px;
    border-radius: 5px;
    border-width: 1px;
    border-color:  #C2B280;
    font-size: 110%;
}

.search-bar:focus{
    border-style:none;
}
{% load static %}
<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.0" />

  <link rel="stylesheet" href="{% static 'styles/main.css' %}" />
  <link rel="icon" href="{% static 'images/logomogo.png' %}" />
  <title>BookShelf</title>

</head>
<body>
    {% include 'navbar.html' %}

    {% block content %}

    {% endblock content %}

</body>
</html>

And here's navbar

{% load static%}
<header>
  <div class="nav-bar">
    <a href="">
      <img src="{% static 'images/logo-white.png' %}" alt="BookShelf Logo" />
    </a>
    <input type="search" class="search-bar" placeholder="Search">
    <nav>
      <ul>
        <li><a href="">Login</a></li>
      </ul>
    </nav>
  </div>
</header>

  <div class="lower-nav-bar">
      <nav>
          <ul>
              <li><a href="main.html">Books</a></li>
              <li><a href="reviews-page.html">Reviews</a></li>
              <li><a href="">Users</a></li>
              <li><a href="">About</a></li>
          </ul>
      </nav>
  </div>

【问题讨论】:

  • 您是否尝试过硬重新加载页面或清除缓存?通常CSS文件不会在每次页面刷新时都更新
  • 可以使用.js静态文件吗?还是任何静态文件?

标签: html css django


【解决方案1】:

在 Firefox 中,我认为在 chrome 中按 F12 可以显示控制台,然后我们可以查看是否所有的 .css 文件都正确加载,如果没有,是什么问题。还可以选择元素并查看应用了哪些 CSS 样式以及它们来自何处。 Django 有一种特定的方式来管理可能被错误配置的静态文件,如果模板中的标签有效,那么问题很可能出在静态文件中。

如果 Django 不能提供静态文件,它可能会在控制台中产生错误消息。

无论如何,我们可能需要模板中的一些代码来查看发生了什么。如果配置正确,它可以加载静态文件,没有问题,但它有一些步骤。 (这里解释https://docs.djangoproject.com/en/4.0/howto/static-files/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2017-06-18
    • 2021-04-30
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多