1

以下是我的 homepage.html 是家庭应用程序

{% extends "base.html" %}
{% load static %}
<link rel = "stylesheet" href = "{% static 'css/home.css' %}" > #reference to stylesheet in the home app static directory

{% block body %}
   <h1>I am homepage</h1>
{% endblock %}

我在项目根文件夹中的base.html如下

<!DOCTYPE html>
<html>
  <head>
    <link rel = "stylesheet" href = "{% static 'base.css' %}" > #stylesheet in root folder
  </head>

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

但是在这里,homepage.html 中的 home.css 不起作用,因为 extend 上的 base.html 在 home.css 进入 head 部分之前关闭了 head。

有什么办法可以将 CSS 添加到标题中

谢谢

4

1 回答 1

2

你只需要另一个块。

在 base.html 中:

<head>
  <link rel="stylesheet" href="{% static 'base.css' %}">
  {% block extrahead %}{% endblock %}
</head>
...

在 homepage.html 中:

{% extends "base.html" %}
{% load static %}
{% block extrahead %}<link rel="stylesheet" href="{% static 'css/home.css' %}">
{% endblock %}
...
于 2018-11-19T13:51:08.573 回答