How to get the CSS rules that a Django Template would use?

Let's just say I have a basic class-based view like:

from django.views.generic import TemplateView

class HomePage(TemplateView):
    template_name = "homepage.html"

and in homepage.html of course we load some CSS, apocryphally:

{% extends "base.html" %}
{% load static %}
{% block CSS %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/default.css' %}" />
    <link rel="stylesheet" type="text/css" href="some CDN based CSS file' %}" />
{% endblock %}

Now I'd like the view to read/load the CSS that will be sent to the client.

If we could just find the source files, we could parse them with cssutils.

And of course it's technically possible to find and parse the template file too, but Django already implements that and has a template loader. Is there any way short of rendering the template into a string, and trying to parse the HTML to extract CSS rules? And even if that's the path we need to pursue, is there a package that will given rendered HTML, and return the CSS rules?

An interesting problem arises from the need, server-side, to extract some CSS information, notably colours.

Back to Top