How to properly extend and include in Django tempates

最后都变了- 提交于 2019-11-30 09:58:30

问题


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 %}

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>

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.

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

Thanks


回答1:


You just need another block.

In base.html:

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

and in homepage.html:

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


来源:https://stackoverflow.com/questions/53375180/how-to-properly-extend-and-include-in-django-tempates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!