我想知道如何在{{}}内添加html:
{{ article.tags.all|join:", " }}
此回归标签由昏迷分隔,例如:sports,cricket,nature等
我想这样做:
{{ <span class="label">article.tags.all|join:", "</span> }}
我想设计每个标签,添加一个span类并删除昏迷
Models.py:
class Tag(models.Model):
name = models.CharField(max_length=255)
description = models.CharField(max_length=255, null=True, default='')
class article(models.Model):
title = models.CharField(max_length=250)
disc = models.TextField(verbose_name="Discription")
posted = models.DateTimeField(auto_now_add=True, editable=False)
updated = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag)
答案 0 :(得分:2)
试试这个:
{% for tag in article.tags.all %}
<span class="label">{{ tag }}</span>{% if not forloop.last %}, {% endif %}
{% endfor %}
在讨论之后:
{% for tag in article.tags.all %}
{% if tag.title == 'hello' %}
Special tag: <span>{{ tag }}</span>
{% else %}
<span>{{ tag }}</span>
{% endif %}
{% endfor %}