简体   繁体   English

Django - css 文件中的模板标签

[英]Django - Template tags inside css file

I'm trying to modify the css of a page based on the PageSetting model that I'm using.我正在尝试根据我正在使用的PageSetting model 修改页面的 css。 I'm allowing the user the ability to change how the theme of a page looks like based on some of the PageSetting 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 .我想做的是根据base_color更改我的 css 中的一些值。 Pretty much something like this:差不多是这样的:

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

  ...

}

is this acheivble in Django?这是在 Django 中实现的吗? I know I can do我知道我能做到

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

but this makes my html ugly and harder to debug.但这使我的 html 变得丑陋且更难调试。

You could do it within a <style></style> tag in your view file 您可以在视图文件的<style></style>标记内执行此操作

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

       ...

    }
</stlye>

There is no way to modify the css itself, you'll have to set the properties in the html. 没有办法修改css本身,你必须在html中设置属性。 I believe a good way to do so is to add a predefined css class to your element. 我相信这样做的一个好方法是为元素添加一个预定义的css类。 That is, in your css, you'll have one class for each style you want, and you'll add the one you want in your html. 也就是说,在你的css中,你将为你想要的每种风格设置一个类,并且你将在你的html中添加你想要的那个。

Code example: 代码示例:

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

Edit: if you have a large number of options (for example, if you want to customize the size of an element), you should use the <style> tag and modify its parameters with your python variables. 编辑:如果您有大量选项(例如,如果要自定义元素的大小),则应使用<style>标记并使用python变量修改其参数。

Code example: 代码示例:

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

do it in label <style> but use "var" in css, so your html does not detect errors在标签<style>但在 css 中使用“var”,因此您的 html 不会检测到错误

<style>
  :root {
    --color: {{object.color}};
  }
  li:before{
    content: "\2022";
    color: var(--color);

  }
</style>

Had the same issue, for me I had to use style tag inside base.html有同样的问题,对我来说我不得不在 base.html 中使用样式标签

<style>
@font-face {
    font-family: "Booter - Zero Zero";
    src: url('../static/fonts/Booter\ -\ Zero\ Zero.woff') format('woff'), url('../static/fonts/Booter\ -\ Zero\ Zero.woff2') format('woff2');

    font-weight: normal;
    font-style: normal;
}

.main-header {
    background-color: rgba(0, 0, 0, .6);
    background-image: url(static/images/header_background.jpg);
    background-blend-mode: multiply;
    background-size: cover;
    padding-bottom: 30px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM