我正在做一个待办事项应用程序我有一个表单,其中一个字段是一个选择字段,您可以选择3 item_importance(重要,正常和不太重要)
在模板中,我想根据项目的重要性更改标签的颜色。问题是我想访问模板中的元组值,但我不知道如何。
model.py:
item_importance = (
('Important', 'Important'),
('Normal', 'Normal'),
('Less Important', 'Less Important')
)
class Item(models.Model):
...code...
item_importance = models.CharField(max_length=25, choices=item_importance)
Form.py:
class CreateItemForm(forms.ModelForm):
description = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'cols': 85}), max_length=200)
deadline_date = forms.DateField(widget=forms.TextInput(attrs= {'type': 'date'}))
class Meta:
model = Item
fields = ["title", "description","item_importance", "deadline_date" ]
模板: 这里if语句错误,我想根据de label的颜色访问元组的值。
{% for item in queryset %}
...code...
<div class="panel-body">
<div class="row">
<div class="col-md-6">
<textarea class="form-control" rows="4" style="background-color:white; resize: none" readonly>{{ item.description }}</textarea>
</div>
<div class="col-md-6">
{% if item_importance == Important %} #How do I access the value 'Important' of the tuple here?
Importancia: <span class="label label-danger"> {{ item.item_importance }}</span>
{% elif item_importance == Normal %} ##How do I access the value 'Normal' of the tuple here?
Importancia: <span class="label label-warning"> {{ item.item_importance }}</span>
{% else %}
Importancia: <span class="label label-success"> {{ item.item_importance }}</span>
{% endif %}
{% endfor %}
<p> Deadline: <strong>{{ item.deadline_date }}</strong> ({{ item.deadline_date|timeuntil }})</p>
</div>
</div>
</div>
</div>
</div>
</div>{% endfor %}
答案 0 :(得分:0)
问题是str
的类型为item_importance
,您要将其与未定义的名称进行比较。此外,您需要在当前循环迭代的item
对象上查找属性{% if item.item_importance == 'Important' %}
...
{% elif item.item_importance == 'Normal' %}
...
{% else %}
...
{% endif %}
。
你应该这样做
#homeBoxShadow{
text-align:center;
width: 300px;
height: 100px;
margin: 0 auto;
background-color: rgba(0,0,0,0.4);
box-shadow: 10px 10px 5px #888888;
color: white;
font-size:3em;
line-height: 90px;
}