【问题标题】:Django: how to pass color as context variable to templatesDjango:如何将颜色作为上下文变量传递给模板
【发布时间】:2020-06-19 07:04:34
【问题描述】:

我需要一些帮助:

我希望我的视图将颜色作为上下文参数传递给模板,以覆盖页面显示的 svg 文件中的默认颜色。

我有一个颜色常量列表:

COLORS = {"W": "#ffffff", "V": "#065636", "B": "#0253A5", "R": "#CC1011"}

我的视图定义中有上下文变量的定义:

view_context = {
    'user_display_name': user.user_display,
    'lightcolor': COLORS[user.lightcolor],
}
return render(request, 'pages/page5.html', context=view_context)

在我的模板 (pages/page5.html) 中,我有一个 <style> 部分来定义 CSS 样式:

<style>
    .X {
        /* fill: #065636 !IMPORTANT; */
        fill: "{{ lightcolor }}" !IMPORTANT;
    }
</style>

.X {fill: #065636 !IMPORTANT;} 工作正常

.X {fill: "{{ lightcolor }}" !IMPORTANT;}

不起作用并被忽略,我猜是因为 lightcolor 是字符串 "#065636" 而不是 RGBcolor #065636

类似地,.X {fill: {{ lightcolor }} !IMPORTANT;} 甚至似乎都不是公认的语法

我在网上找不到任何相关的东西

如何将颜色作为上下文参数传递到模板的 &lt;style&gt; 部分并使其正常工作?

【问题讨论】:

  • fill 属性用于setting the color of an SVG shape。这就是你想要做的吗?
  • 是的——正如我所说,直接使用颜色编码,代码按我的意愿执行。用变量替换颜色编码时出现问题

标签: django django-views django-templates


【解决方案1】:

尝试使用内联样式。

例如

<h4 style="color:{{ color }}">Records</h4>

【讨论】:

  • 当我试图覆盖 SVG 文件中的颜色时,这是不可能的
【解决方案2】:

fill 属性是 for setting the color of an SVG shape。假设这就是你想要做的,你的语法应该可以工作。

工作演示:https://repl.it/@JoshG5/Django-Template

views.py

from django.http import HttpResponse
from django.shortcuts import render


def index(request):
  COLORS = {"W": "#ffffff", "V": "#065636", "B": "#0253A5", "R": "#CC1011"}
  view_context = {
    'user_display_name': "foo",
    'lightcolor': COLORS["V"],
  }
  return render(request, 'page.html', context=view_context)

templates/page.html

<style>
    .X {
        /* fill: #065636 !IMPORTANT; */
        fill: {{ lightcolor }} !IMPORTANT;
    }
</style>
<svg height="250px" width="600px" class="X"
      xmlns="http://www.w3.org/2000/svg"
      version="1.1"> 
      <circle class="opacity1" cx="100"
        cy="100" r="50" /> 
      <circle class="opacity2" cx="250"
        cy="100" r="50" /> 
      <circle class="opacity3" cx="400"
        cy="100" r="50" /> 
      <circle class="opacity4" cx="550"
        cy="100" r="50" /> 
</svg>

您应该会看到以所需颜色呈现的圆圈。

如果您尝试填充块或内联元素,请尝试使用color 属性:

.X {
    color: {{ lightcolor }} !important;
}

【讨论】:

  • 目标是重新着色 svg 形状。所以填充是要走的路。此外,它确实适用于 .X { color: #065636 !important;} 。我遇到的主要问题是,与您的工作演示中发生的情况相反,在我的计算机上,既不是 .X { color: {{ lightcolor }} !important;} 也不是 .X { color: "{{ lightcolor }}" !important ;} 正在工作中。结果与 .X { color: "#065636" !important;} : no recoloring 相同
  • 您是否会偶然知道我如何将“#065636”转换为相应的#065636 RGBcolor 对象?
  • 好的,我找到了问题的根源!我正在 PC 上处理 VS 代码。当我保存时,格式从 .Y { fill: {{darkcolor}} !IMPORTANT; }
【解决方案3】:

好的,我找到了问题的根源!我正在 PC 上处理 VS 代码。 当我保存时,我的 html 文件的格式从:

.Y {
        fill: {{darkcolor}} !IMPORTANT;
    }

进入:

    .Y {
        fill: {
                {
                darkcolor
            }
        }

         !IMPORTANT;
    }

填充后的大括号也标记为红色,表示我的语法存在问题(保存和代码重组前后)

如果我设法在 VS 代码之外修改我的文件...一切都很好,一切都在正常工作!

谁能解释我如何避免重新组织这部分代码? (是因为我的 linter,还是因为其他原因?停止 linting 似乎并没有改变任何东西)

【讨论】:

  • 好的。我尝试了其他一些事情......最后成功地让我的代码杂乱无章并且指令填充:{{darkcolor}}!IMPORTANT;放在几行。我在 VScode 中安装了 Django 扩展(batisteo.vscode-django),它正确格式化了 html 代码以包含 Django 模板符号
猜你喜欢
  • 2014-03-10
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 2015-08-10
  • 2021-02-13
相关资源
最近更新 更多