6

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.

1
  • did you find any solution to the above query as i am also having a similar requirement. Commented Mar 30, 2019 at 5:49

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

That's not the way to do this sort of thing. You should define different classes in the CSS file, then use then in your template dependent on the variables there.

1 Comment

I am sorry, I am confused. How do I define my own css classes and use it? Please see the Edit.
3

you can pass the css style attribute adding the following line in your html page.

 <div class="profile-cover" style="background: url('{{ cover }}')">

added attribute to css class provile-cover style="background: url('{{ cover }}')"

{{ cover }} is the variable rendered from views.py to the HTML page using the urls.py

2 Comments

This doesn't seem to work for me. What if you need to pass a color from the View like background-color : {{bgcolor}};, as the question asked?
this will be correct way to use it background-color : "{{bgcolor}}"
1

I agree with Daniel Roseman's answer, but if you -really- need to do this you can just define your CSS in the template and use the python variable as shown there.

Another option is to see if you can use mako templating if you can get it to work with Django.

But, unless you have some unusual compelling reason you need to do this, define your own CSS classes.

6 Comments

Ok... But I am confused. How do I define my own css classes and use it?
It's a fundamental part of CSS. See cssbasics.com/chapter_3_css_class.html for explanation.
I'm also going to throw this one at you, because it's how I and many others learned the basics of HTML/CSS w3schools.com
Oh! I am sorry. I have edited above. What I meant was, how do I 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. I hope, I made myself clear. Please guide me if there's a way to achive this.
If you mean different skins, try this: github.com/lethain/django-userskins. If you mean something else, like a wysiwyg editor, I'm not sure, I'm 90% a backend developer.
|

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.