CSS 没有连接到 HTML (Django)

CSS doesn't connect to HTML (Django)

None 个静态 css 文件连接到 html。但是,所有静态图片都可以正常工作。

settings.py

DEBUG = True
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main'
]
import os
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

base.html (parent)

<!DOCTYPE html>
{% 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="shortcut icon" type="image/png" href="{% static 'img/favico.png' %}">
  <title>{% block title %}{% endblock %}</title> 
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <link rel = 'stylesheet' href = "{% static 'css/main.css' %}">
</head>
<body>
  <div class="d-flex flex-column flex-md-row align-items-center pb-3 mb-4 border-bottom">
    <font style="font-size: 20px; opacity: 0.9; font-weight: lighter;">
      <a href="{% url 'home' %}" class="d-flex align-items-center text-dark text-decoration-none">
        <img class="main_img" width="25px" height="30px" src="{% static 'main/img/favico5.png' %}">
        <span class="logo" style="padding: 5px"><font style="font-family: Arial">Teach me</font></span>
      </href>
   </font>
      <nav class="d-inline-flex mt-2 mt-md-0 ms-md-auto">
        <a class="me-3 py-2 link-secondary text-decoration-none" href="{% url 'home' %}"><font style="padding-right: 10px;">Главная</font></a>
        <a class="me-3 py-2 link-secondary text-decoration-none" href="{% url 'tutors' %}"><font style="padding-right: 10px;">Репетиторы</font></a>
        <a class="me-3 py-2 link-secondary text-decoration-none" href="{% url 'articles' %}"><font style="padding-right: 21px;">Статьи</font></a>
        <a class="me-3 py-2 link-secondary text-decoration-none" href="{% url 'login' %}"><font style="background-color:#FFEFDF; border: 10px solid #FFEFDF; color: black; border-radius: 10px; border-width: 3px 20px 7px 20px; box-shadow: 1px 3px 1px #A9A9A9;">Войти</font></a>
      </nav>

  </div>
    {% block content %}{% endblock %}
</body>
</html>

index.html

{% extends 'main/base.html' %}
{% load static %}
<head>
  {% block title %}
  {{title}}
  {% endblock %}
  <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}">
</head>
{% block content %}
<main>
  <h1>GHBDTN</h1>
  <body>
    <div class="grid-wrapper">
      <header class="grid-header">
          <img class="circles" src="{% static "main/img/main8.jpg" %}" alt="main pic">
          <p>Предоставим качественное образование, <br>
          поможем понять школьную программу, <br>
          улучшить оценки и <br>
          подготовиться к экзаменам</p>
      </header>
    </div>
  </body>
</main>
{% endblock %}

style.css (relate to index.html)

.grid-wrapper {
    display: grid;
    grid-template-columns: 1 fr;
    grid-template-rows: 1 fr;
    grid-template-areas: 'header header';
}

.circles {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

header {
    position: relative;
    width: 100%;
}

p {
    color: red;
    text-align: center;
    position: absolute;
    width: 100%;
    text-align: center;
    top: 0;
}

main.css(与base.html相关)

nav {
    font-family: Montserrat;
    font-size: 20px;
}

nav a:hover {
    color: red;
    transition: all .6s ease;
    transform: scale(1.10);
}

我已将 BASE_DIR 更改为文档一,但没有任何更改。显示所有 html。我用 css 创建了 html-file 而没有 django 可以正常工作

我的项目结构如下:

/project 
//taskmanager
///main 
////static 
/////css
//////main.css and style.css
////templates
/////main
//////base.html
//////index.html

像这样
base.html

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
  <!--- Add all your meta tags css files --->
  <title>{% block title %}{% endblock %}</title> 
  {% block extra_head_links %}
  {% endblock %}
</head>
<body>
 
    {% block content %}{% endblock %}
</body>
</html>

在你的index.html

里面
{% extends 'main/base.html' %}
{% load static %}

{% block title %}
  {{title}}
{% endblock %}

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

{% block content %}
<main>
  <h1>GHBDTN</h1>
  <body>
    <div class="grid-wrapper">
      <header class="grid-header">
          <img class="circles" src="{% static "main/img/main8.jpg" %}" alt="main pic">
          <p>Предоставим качественное образование, <br>
          поможем понять школьную программу, <br>
          улучшить оценки и <br>
          подготовиться к экзаменам</p>
      </header>
    </div>
  </body>
</main>
{% endblock %}

以上代码将从 base.html 扩展所有 css 并且会在 [=] 上添加额外的 css link 18=]index.html