没有在django项目中加载CSS文件

CSS file is not loading in django project

本文关键字:加载 CSS 文件 项目 django      更新时间:2023-09-26

我已经为所有css文件添加了静态文件链接。仍然只有HTML文件显示在浏览器中。CSS文件不起作用。如果有什么不对劲,请告诉我。如何纠正?

我转到index.html页面的页面源代码,然后单击CSS链接。它不显示CSS文件。它显示了一些其他的HTML文件。我得到错误:error page not found

index.html

<head>{% load static %}
  <link rel="stylesheet" href="{% get_static_prefix as STATIC_PREFIX %} /static/style1.css" type="text/css" />
  <title>Login Page</title>
</head>
<body>
  <form action="/loginpage/" name="myForm" method="post">
    {% csrf_token %}
    <fieldset>
      <legend>Login</legend>
      <br>
      <p style="display:inline">Username</p>
      <br>
      <br>
      <input type="text" name="user" autocomplete="off" placeholder="username" size="30" autofocus required/>
      <br>
      <br>
      <p style="display:inline">Password</p>
      <br>
      <br>
      <input type="password" name="pass" autocomplete="off" placeholder="password" size="30" required/>
      <br>
      <br>
      <input type="submit" name="submit" value="Login" style="height:30px; width:70px" />
      <br>
      <br>
      <p style="font-family:arial; color:#FFFFFF;">{{message}}</p>
    </fieldset>
  </form>
  <br>
  <br>
  <a href="/formlink/" id="sign">Create account</a>
</body>

style1.css

legend {
    border: medium none;
    left: 39%;
    margin: 0 auto;
    padding: 0 10px;
    position: relative;
    text-align: center;
    width: auto;
    color: #FFFFFF;
    font-size: 18px;
    font-weight: bold;
    font: normal 130% 'century gothic', arial, sans-serif;
}
body  {
    background-image: url("12.jpg");
}
fieldset {
    position: absolute;
    left: 600px;
    top: 280px;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
}
form {
    text-align: center;
}
p {
    font: normal 100% 'century gothic', arial, sans-serif;
    color: #FFFFFF;
}
#home {
    background-color: grey;
    color: #ffffff;
    display: inline-block;
    font-size: 16px;
    margin-left: 20px;
    opacity: 0.9;
    padding: 14px 25px;
    position: absolute;
    text-decoration: none;
    font: normal 100% 'century gothic', arial, sans-serif;
    left: 1450px;
    top: 100px;
}
#home:hover {
    color: black;
    text-decoration: underline;
}
#sign {
    background-color: grey;
    color: #ffffff;
    display: inline-block;
    font-size: 16px;
    margin-left: 20px;
    opacity: 0.9;
    padding: 14px 25px;
    position: absolute;
    text-decoration: none;
    font: normal 100% 'century gothic', arial, sans-serif;
    position: absolute;
    left: 680px;
    top: 650px;
}
#sign:hover {
    color: black;
    text-decoration: underline;
}

您已经花了很大的力气来尝试获得静态前缀,然后添加到我认为是静态目录的目录上,无论如何,您只需要使用static标签

{% get_static_prefix as STATIC_PREFIX %} /static/style1.css

应该是

{% static 'style1.css' %}