Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确扩展和包含在Django tempates中_Django_Templates_Django Static - Fatal编程技术网

如何正确扩展和包含在Django tempates中

如何正确扩展和包含在Django tempates中,django,templates,django-static,Django,Templates,Django Static,以下是我的主页。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> {%

以下是我的主页。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 %}
{%extends“base.html”%}
{%load static%}
#对home app静态目录中样式表的引用
{%block body%}
我是主页
{%endblock%}
项目根文件夹中的My base.html如下所示

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

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

#根文件夹中的样式表
{%block content%}
{%endblock%}
但是在这里,home.html中的home.css不起作用,因为extend上的base.html在home.css进入head部分之前关闭了head

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


谢谢

你只需要再加一块就行了

在base.html中:

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

{%block extrahead%}{%endblock%}
...
在homepage.html中:

{% extends "base.html" %}
{% load static %}
{% block extrahead %}<link rel="stylesheet" href="{% static 'css/home.css' %}">
{% endblock %}
...
{%extends“base.html”%}
{%load static%}
{%block extrahead%}
{%endblock%}
...

嗨,丹尼尔,我已经试过了。但是,由于extrahead位于base.html中,因此它会在home.css的根目录中查找静态目录。它不会在home应用程序静态目录中搜索home.css。我唯一能做的就是将home.css放在根目录中。我的问题是,;这是我能解决这个问题的最好办法吗?这与我有关,因为我最终会将所有CSS放在根目录中。这与此无关。如果要将CSS放在应用程序目录中,请执行以下操作:
{%static'CSS/myapp/home.CSS%}
。它当然不受基础模板所在位置的影响。