Guide to implementing multiple CSS files in Django template inheritance

Seeking a solution for incorporating multiple CSS files for different templates using Django template inheritance.

Currently, I have extended my base.html template with another app's template. Now, I want to use separate CSS files for each app's HTML templates (one app = 1x HTML, 1x JS, 1x CSS). However, when including the template within the template tag, it replaces the CSS file from the base.html and causes a site crash.

How can one add a second CSS file into an extended HTML template that only affects the extended part without touching the base.html?

Your guidance is highly appreciated in advance.

Extended HTML template causing site crash due to replacement of base.css file:

{% extends 'templates/base.html' %}
{% load static %}

{% block head_css_site %}
<link href="{% static 'Quotes_app.css' %}" rel="stylesheet" type="text/css">
{% endblock head_css_site %}

{% block content %}
<h1>Test</h1>

{% endblock %}

{% block footer_javascript_site %}
<script src="{% static 'Quotes_app.js' %}"></script>
{% endblock footer_javascript_site %}

base.html:

{# HTML5 declaration #}
<!DOCTYPE html>
{% load static %}
<html>
{# Make modifiable head elements #}

<head>
  <title>{% block title %}  {% endblock title %} DASHEX </title>
  {% block head_favicon %}
  <link rel="icon" type="image/png" href="{{ STATIC_URL }}images/logo.jpg">
  {% endblock head_favicon %}
  {% block head_meta %}
  {% block head_meta_charset %}
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  {% endblock head_meta_charset %}
  {% block head_meta_contentlanguage %}
  <meta http-equiv="Content-Language" value="en-US" />
  {% endblock head_meta_contentlanguage %}
  {% block head_meta_viewport %}
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  {% endblock head_meta_viewport %}
  {% endblock head_meta %}


  {% block css %}
  {% block head_css_site %}
  <link href="{% static 'base.css' %}" rel="stylesheet" type="text/css">
  {% endblock head_css_site %}
  {% endblock css %}


</head>
[...]

Project Structure:

https://i.sstatic.net/GQUmP.jpg

Answer №1

To enhance the functionality of your template, include the code {{ block.super }} (look for "super") within the block override of the deriving template. This allows you to utilize both the parent content and your custom additions:

{% block head_css_site %}
{{ block.super }}
<link href="{% static 'Quotes_app.css' %}" rel="stylesheet" type="text/css">
{% endblock head_css_site %}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The navigation bar is missing from the screen

Where is the navbar in this snippet? Did I overlook something important? If so, what? <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="navbar nav ...

"Embedding a Highcharts web chart within a pop-up modal window

I am looking to incorporate a Highcharts web chart onto a specific part of a Bootstrap modal, while ensuring the size remains dynamic along with the modal itself. Here's what I have attempted so far: Sample HTML code: <td><a href="#">${ ...

Difficulty adapting CSS using JavaScript

I am looking to adjust the padding of my header to give it a sleeker appearance on the page. I attempted to achieve this with the code below, but it seems to have no effect: function openPage() { var i, el = document.getElementById('headbar' ...

Is there a way to change the entire background color of my popover?

Hi there! My issue is that the arrow in my popover isn't changing color and is still showing up as white. Here is the current code: http://jsfiddle.net/GZSH6/19/ Here is the CSS code: .popover { background: #BE7979; color: white; borde ...

Display additional tiles within a compact container

I'm attempting to replicate the user interface used by foursquare! Positioning the map slightly off center like they have . I've figured out how to do one part but struggling with the second part. Initially, I loaded the map in a small div (t ...

Can floating elements be disregarded by block elements?

According to W3C, the behavior of floating elements is such that: When a float is present, non-positioned block boxes that come before and after the float flow vertically as if the float doesn't exist. However, line boxes positioned next to the fl ...

I was using a music application to showcase album details immediately after they were created in reverse order. However, I kept encountering the following error. Can someone please help me troubleshoot this

When trying to open the details of a created album, an error occurred with the following traceback: File "C:\Users\Sanath\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 55 a ...

Show the chosen option when the textarea is no longer in focus

My form includes a text box and a button: <p><textarea rows="4" cols="30">Aliquam erat volutpat.</textarea></p> <p><input type="button" value="Submit"></p> When the user selects text in the textarea and then cl ...

Django: Show the first and last names

I am in the process of developing an application that allows users to log in, perform various actions, and view their personal details such as first name, last name, email, username, and more. Currently, the application only displays the username and email ...

What is the best way to horizontally align a div within a parent div?

I'm having trouble centering the red div containing three child elements (h1, h3, and button) within a green container div using CSS. I initially attempted to center the red div vertically and horizontally on its own, but after some research, I learne ...

Add a <div> element to a webpage in a random location every time the page is refreshed

I have a variety of 2 types of <div>s displayed on a single page. Collection 1 consists of: <div class="feature">content 1</div> <div class="feature">content 2</div> ...and so forth. Collection 2 consists of: <div class ...

Custom WordPress Popups

I have implemented a custom popup on my WordPress website and it works fine when tested on a simple `index.html` file on the local server. However, when I embed the code in the `header.php` and `style.css` files of WordPress on the local server, the popup ...

Left-align the tab elements

I am looking to customize this example for Vertical tabs. Sandbox: https://codesandbox.io/s/v0p58 My goal is to have the tabs aligned on the left side. I attempted to include: alignItems: 'left' tabs: { borderRight: `1px solid ${theme.palet ...

Real-time Feedback: Providing live updates to users about the current connection status

My task requires pulling data from multiple datasources which can be time-consuming. To enhance user experience, I want to provide real-time information about the progress by displaying messages like "Currently retrieving data from table1" and "Now fetchin ...

Issue with Relative Div Rendering Correctly in Firefox and IE

I have a picture nested in two divs and I wanted to overlay a piece of tape in the corner of the picture. I created a div for the tape image and positioned it at the bottom of the document with the following attributes: #tape { width: 100px; heig ...

The Django adminplus link vanishes without a trace once Grappelli is activated

Recently, I implemented adminplus to automatically generate a link on the admin page to my custom view. For example, using the code admin.site.register_view('somepath', 'My Fancy Admin View!', view=my_view) should create a 'Custom ...

Leveraging knockout.js to define the width for kendo-ui input wrappers

In the durandal project I'm working on, where JavaScript and HTML are written on separate pages, I encountered an issue with a kendo-combo element. When I initially set the width using the wrapper-input width declaration, it worked perfectly fine. How ...

Modify current HTML code for 1027x768 displays

Looking for assistance with a webpage displayed at http://s2.ralfgroup.com. It appears correctly on screens with a resolution of 1280x1024, but extends beyond the screen on screens with a resolution of 1027x768. The issue seems to be related to the naviga ...

Building a drop down menu with HTML and CSS: a step-by-step guide

Looking for a way to create a dropdown menu for the list item with the id "gameDesignPage" in my HTML structure: <div class="MenuContainer"> <div class="Menu"> <div class="MenuContents"> <ul> ...

I'm curious as to why "Django HTML" is not included in the "Select Language Mode" options in VSCode on Windows 10. Can anyone shed some light on this?

OS: Windows 10, 64-bit Text Editor: VSCode 1.55.2 Python Version: 3.9.0 Django Version: 3.2 Currently, I am enrolled in a Python course by codewithmosh. Below is a screenshot of my folders and files. https://i.sstatic.net/u99R7.png After clicking on th ...