【发布时间】:2017-07-17 10:22:24
【问题描述】:
models.py:
class MyText(models.Model)
value = models.TextField()
appearance = models.Charfield(
max_length=50,
choices=(
('bold', 'Bold'),
('italic', 'Italic'),
)
)
对象:
a_lot_of_text = MyText(value='a lot of text', appearance='bold')
我通过context 中的views.py 将这个对象传递给HTML 模板。我想检查(在 HTML 中)a_lot_of_text 有什么样的外观,并使用 certan class 作为其 <div> 元素。换句话说,我想得到这样的东西:
mytemplate.html(伪代码):
<style>
bold_class {...}
italic_class {...}
</style>
{% if object.appearance == 'bold' %}
{% somehow i will use 'bold_class' %}
{% elif object.appearance == 'italic' %}
{% somehow i will use 'italic_class' %}
{% endif %}
{% for word in object.value %}
<div class="{{class_that_i_have_chosen_in_if-block}}">{{word}}</div>
{% endfor %}
因为a_lot_of_text 中有很多word,所以我想在for-block 之前检查我的班级1 次并在那里使用它。我想我可以制作my own assignment Tag - 这是正确的解决方案吗?
【问题讨论】: