Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 如何向Django模板添加字符串?_Css_Django_Templates_Inline - Fatal编程技术网

Css 如何向Django模板添加字符串?

Css 如何向Django模板添加字符串?,css,django,templates,inline,Css,Django,Templates,Inline,我想把这段代码 <section data-name="color1" data-type="color" data-order="2" style="color:{{ block.color1|default:"inherit" }}"> 默认情况下在html中输出此标记 <section data-name="color1" data-type="color" data-order="2" style="color: rgb(0, 0, 0);"> 由于我

我想把这段代码

<section data-name="color1" data-type="color" data-order="2" style="color:{{ block.color1|default:"inherit" }}">

默认情况下在html中输出此标记

<section data-name="color1" data-type="color" data-order="2" style="color: rgb(0, 0, 0);">

由于我的一个子元素上存在颜色冲突,因此未应用block.color1变量的颜色。我想在内联CSS的输出中添加一个“!important”来解决这个问题。换句话说,我想要一个

<section data-name="color1" data-type="color" data-order="2" style="color: rgb(0, 0, 0) !important;">


在HTML中。我可以向Django添加什么,以确保我收到带有的输出!在CSS中很重要?

您可以添加另一个过滤器,假设第一个过滤器返回字符串:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def mark_important(value):
    return value.replace(';', '!important;')
在模板中:

<section data-name="color1" data-type="color" data-order="2" style="color:{{ block.color1|default:"inherit"|mark_important }}">

您可以添加另一个过滤器,假设第一个过滤器返回字符串:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def mark_important(value):
    return value.replace(';', '!important;')
在模板中:

<section data-name="color1" data-type="color" data-order="2" style="color:{{ block.color1|default:"inherit"|mark_important }}">

您只需向该部分添加一个类:

<section data-name="color1" data-type="color" data-order="2" class="{{ foo }}">

您只需向该部分添加一个类:

<section data-name="color1" data-type="color" data-order="2" class="{{ foo }}">