Django - Template tags inside css file

I am trying to change the CSS of a page based on the model PageSettingI'm using. I allow the user to change the theme style based on some attributes PageSetting.

class PageSettings(SingletonModel):
    theme = models.IntegerField(choices=THEMES,
                                verbose_name=_("Theme"),
                                default=0)
    base_color = RGBColorField(blank=False,
                               verbose_name=_("Base color"),
                               default="bc0000")

    ...

What I would like to do is change some values ​​in my css depending on base_color. Pretty much something like this:

# inside my_theme.css
.side-bar {
  background: {{ pagesetting.base_color }};
  float:left;
  width: {{ pagesetting.sidebar_width}}%

  ...

}

is this in django? I know what I can do

<div class="sidebar" style='background-color:#{{pagesetting.base_color}};'>

but it makes my html ugly and harder to debug.

+4
source share
2 answers

You can do this in a tag <style></style>in your view file

<style>
    .side-bar {
       background: {{ pagesetting.base_color }};
       float:left;
       width: {{ pagesetting.sidebar_width }};

       ...

    }
</stlye>
0
source

css, html. , - CSS . , css, , , , html.

:

<div class={{ my_style_class_red }}>
 ....
</div>

: (, ), <style> python.

:

<style>
   h1 {color:{{my_color}};}
   p {color:blue;font-size:{{my_font_size}};}
</style>
0

Source: https://habr.com/ru/post/1621173/


All Articles