5

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 ?

1 Answer 1

18

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 %}
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.