[英]how to color text with condition in django?
I have a django app and I want to color some text, if that text is true in dictionary.我有一个 django 应用程序,如果该文本在字典中为真,我想给一些文本上色。
So I have this method:views.py所以我有这个方法:views.py
def data_compare2(request):
template = get_template("main/data_compare.html")
dict2 = {"appel": 3962.00, "waspeen": 3304.07, "ananas":24}
context = {"dict2": dict2}
res = dict((v,k) for k,v in dict2.items())
return HttpResponse(template.render(context, request, res[3962.00]))
and this is the template:这是模板:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container center">
{% for key, value in dict1.items %}
{%if {{res}} %}
<div style="background-color:'red'"></div>
{%endif%}
{{ key }} {{value}}<br>
{% endfor %}
</div>
</body>
</html>
So the text appel": 3962.00 has to appear in red.所以文本 appel": 3962.00 必须显示为红色。
Question: how to make the founded text in dictionary red?问题:如何将字典中已创建的文字设为红色?
I would separate fruits from the condition, inside the context.我会在上下文中将水果与条件分开。 Transform the condition into a list to check on the template.
将条件转换为列表以检查模板。
views.py视图.py
def data_compare2(request):
fruits = {"appel": 3962.00, "waspeen": 3304.07, "ananas":24,}
condition = ['appel', 'ananas']
context = {
'fruits': fruits,
'condition': condition
}
return render(request, 'main/data_compare.html', context)
template.html模板.html
{% extends 'base.html' %}
{% block content %}
<div class="container center">
{% for key, value in fruits.items %}
<span {% if key in condition %} style="color: red;" {% endif %}>{{ key }}: {{value}}</span><br>
{% endfor %}
</div>
{% endblock %}
This will result in 'appel' and 'ananas' being red.这将导致“appel”和“ananas”变为红色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.