Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables to CSS file in Django

Is it possible to pass variables in CSS files, like in HTML file. Example:

In views.py:

def home(request):
    bgcolor = "#999"
    ...
    ...

In the CSS file:

body {
    background-color : {{bgcolor}};
}

If yes, can you please guide me how to achieve this? I would really appreciate. Thank you!

Edit: Ok, my bad. What I meant was, how do let users customize their page if they wanted to? I suppose I could have done this without using external stylesheet, but I think CSS is served fastest if it's static and on a CDN and not using a template and CPU resources to render. Please guide me if there's a way to achive this.

like image 906
Robin Avatar asked Aug 25 '13 17:08

Robin


1 Answers

This is a old question but I thought I'd help others as I also wanted to do this and I found a solution.

Instead of using a normal link tag specifying your stylesheet, use style tags and include your css file inline.

<style>{% include 'my.css' %}</style>

Then you can put jinja code in your css file and it will render the variables. Useful if you want to allow users to customize colors etc.

like image 136
h82fail Avatar answered Sep 30 '22 09:09

h82fail