【问题标题】:Is it possible to pass variables to CSS file in Django是否可以将变量传递给Django中的CSS文件
【发布时间】:2020-10-11 03:22:32
【问题描述】:

我想知道是否可以将变量从 Django 视图传递到 CSS 文件?有什么办法吗? 我正在关注前端设计中的一些教程,想知道它是否可以在 Django 中动态构建?

.card:nth-child(1) svg circle:nth-child(2){
  stroke-dashoffset: calc(440 - (440 * 90) / 100);
  stroke: #00ff43;
}

.card:nth-child(2) svg circle:nth-child(2){
  stroke-dashoffset: calc(440 - (440 * 80) / 100);
  stroke: #00a1ff;
}

.card:nth-child(3) svg circle:nth-child(2){
  stroke-dashoffset: calc(440 - (440 * 60) / 100);
  stroke: #ff04f7;
}
stroke-dashoffset: calc(440 - (440 * {{ var }}) / 100);

我可以将这些变量从视图传递给 CSS 中的这个 var 吗? 这部分代码分别给出 90、80 和 60%。

【问题讨论】:

    标签: css django django-views


    【解决方案1】:

    如果 css 在您的 <head> 中,您可以在 css 中传递您的变量,例如:

    <head>
      <style>
      .card:nth-child(1) svg circle:nth-child(2){
        stroke-dashoffset: calc(440 - (440 * {{ var }}) / 100);
        stroke: #00ff43;
      }
      </style>
    </head>
    

    但是你不能将它直接传递给你的外部 css 文件。

    在这种情况下,您有一个 base.html:

    <head>
      {% bloc extra_style %}
      {% endblock %}
    </head>
    

    在你的模板中:

      {% bloc extra_style %}
      <style>
      .card:nth-child(1) svg circle:nth-child(2){
        stroke-dashoffset: calc(440 - (440 * {{ var }}) / 100);
        stroke: #00ff43;
      }
      </style>
      {% endblock %}
    

    【讨论】:

    • 好的,但是我将如何渲染两个模板,即 base.html 和 index.html ?
    • 我已经根据你的问题更新了我的答案,看看,希望对你有帮助。
    猜你喜欢
    • 2013-08-28
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2018-03-16
    • 2014-07-12
    • 2021-01-01
    相关资源
    最近更新 更多