英文:
Problem with tags in Django | used taggit for storing tags
问题
以下是您要翻译的内容:
Template file:
<div id="tab-2" class="tab-pane fade show p-0">
{% for event in events %}
{% for tag in event.tags.all %}
{% if tag == 'Tech' %}
<div class="row g-3">
<div class="col mb-5">
<div class="d-flex h-100">
<div class="flex-shrink-0">
<img class="img-fluid" src="{{event.e_image.url}}" alt="" style="width: 430px; height: 350px;">
<h4 class="bg-dark text-primary p-2 m-0">{{event.e_date}}</h4>
</div>
<div class="d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4">
<h5 class="text-uppercase">{{event.e_name}}</h5>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
class Events(models.Model):
event_id = models.AutoField(primary_key=True, max_length=10)
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
e_name = models.CharField(max_length=50)
e_image = models.ImageField(null=True, blank=True, upload_to='eventimg')
e_date = models.DateTimeField()
e_url = models.URLField(null=True, blank=True)
tags = TaggableManager()
def __str__(self) -> str:
return self.e_name
"When I try {% if tag == 'Tech' %} to display block only when tag is equal to Tech then its not workin. I mean that Block is not at all visible.
There are multiple tags in a single event thus I am looping event.tags.all"
I already had installed, imported and mentioned taggit in settings, its working properly. But I am stucked in above situation.
英文:
Template file:
<div id="tab-2" class="tab-pane fade show p-0">
{% for event in events %}
{% for tag in event.tags.all %}
{% if tag == 'Tech' %}
<div class="row g-3">
<div class="col mb-5">
<div class="d-flex h-100">
<div class="flex-shrink-0">
<img class="img-fluid" src="{{event.e_image.url}}" alt="" style="width: 430px; height: 350px;">
<h4 class="bg-dark text-primary p-2 m-0">{{event.e_date}}</h4>
</div>
<div class="d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4">
<h5 class="text-uppercase">{{event.e_name}}</h5>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
class Events(models.Model):
event_id = models.AutoField(primary_key=True, max_length=10)
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
e_name = models.CharField(max_length=50)
e_image = models.ImageField(null=True, blank=True, upload_to='eventimg')
e_date = models.DateTimeField()
e_url = models.URLField(null=True, blank=True)
tags = TaggableManager()
def __str__(self) -> str:
return self.e_name
When I try {% if tag == 'Tech'%} to display block only when tag is equal to Tech then its not workin. I mean that Block is not at all visible.
There are multiple tags in a single event thus I am looping event.tags.all
I already had installed, imported and mentioned taggit in settings, its working properly. But I am stucked in above situation.
答案1
得分: 1
在查看 GitHub 代码后,有一个 name
字段,所以应该是 {% if tag.name == 'Tech' %}
,如下所示:
<div id="tab-2" class="tab-pane fade show p-0">
{% for event in events %}
{% for tag in event.tags.all %}
{% if tag.name == 'Tech' %}
<div class="row g-3">
<div class="col mb-5">
<div class="d-flex h-100">
<div class="flex-shrink-0">
<img class="img-fluid" src="{{event.e_image.url}}" alt="" style="width: 430px; height: 350px;">
<h4 class="bg-dark text-primary p-2 m-0">{{event.e_date}}</h4>
</div>
<div class="d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4">
<h5 class="text-uppercase">{{event.e_name}}</h5>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
tag
是 Tag
模型的实例,因此您不应该直接将其与字符串进行比较。
英文:
After looking at GitHub code, there is a name
field so it should be {% if tag.name == 'Tech' %}
instead, like so:
<div id="tab-2" class="tab-pane fade show p-0">
{% for event in events %}
{% for tag in event.tags.all %}
{% if tag.name == 'Tech' %}
<div class="row g-3">
<div class="col mb-5">
<div class="d-flex h-100">
<div class="flex-shrink-0">
<img class="img-fluid" src="{{event.e_image.url}}" alt="" style="width: 430px; height: 350px;">
<h4 class="bg-dark text-primary p-2 m-0">{{event.e_date}}</h4>
</div>
<div class="d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4">
<h5 class="text-uppercase">{{event.e_name}}</h5>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
</div>
The tag
is an instance of the Tag
model, so you should not compare it directly with string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论