1

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

jeff
  • 910
  • 2
  • 6
  • 25

1 Answers1

2

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 %}
...
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Hi Daniel, I have tried that. However, since the extrahead is in the base.html, it looks for the static directory in the root for the home.css. It does not search for the home.css in the home application static directory. The only thing I could do is to have the home.css in the root directory. My question is; is that the best thing that I can do to solve this? It concerns me as I would end up in having all the CSS in the root directory – jeff Nov 19 '18 at 14:00
  • No, that is nothing to do with this. If you want to put your CSS in the application directory, then do so: `{% static 'css/myapp/home.css' %}`. It is certainly not affected by where the base template is. – Daniel Roseman Nov 19 '18 at 14:04