Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Use multiple CSS file in one html

In Django, it is possible to use different Css files in one HTML document ?

I would like to use one css for base.html and another one for page1.html while expanding base.html to page1.html...

For example, base.html :

{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{% static "css/base.css" %}">
</head>
{% block content %}{% endblock%}
</body>
</html>

and page1.html :

{% extends "base.html" %}
{% load static %}
<link rel="stylesheet" href="{% static "css/page1.css" %}">
{% block content %}
code...
{% endblock %}

I don't want to merge the Css files, do I have an another solution ?

like image 287
Eddie.W Avatar asked Apr 17 '18 08:04

Eddie.W


1 Answers

You can use as many CSS files as you like, of course.

The best thing to do here is to define a specific block inside your base template's <head> section for extra CSS, or any other content you might want to put there. So:

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

Then your child template can be:

{% extends "base.html" %}
{% load static %}
{% block extrahead %}
<link rel="stylesheet" href="{% static "css/page1.css" %}">
{% endblock %}
...
like image 121
Daniel Roseman Avatar answered Oct 09 '22 20:10

Daniel Roseman