如何正确扩展并包含在Django模板中 [英] How to properly extend and include in Django tempates

查看:103
本文介绍了如何正确扩展并包含在Django模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following is my homepage.html is home app

{% 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是以下

My base.html in the project root folder is the following

<!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不能正常工作,因为base.html上的extension在home.css可以进入头部之前关闭了头部.

But here, the home.css in the homepage.html is not functional as the base.html on extend closes the head before the home.css can come in the head section.

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

Is there any way I can add the CSS into the header

谢谢

推荐答案

您只需要另一个块即可.

You just need another block.

在base.html中:

In base.html:

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

和homepage.html中的

and in homepage.html:

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

这篇关于如何正确扩展并包含在Django模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆