[FIXED] Problem with tags in Django | used taggit for storing tags


{% for events in events %} {% % of tags in event.tags.all} {% if tag == 'Tech' %}


{% endif %} {% end for %} {% end for %}

Class event (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)
    Tag = TaggableManager()

    def __str__(self) -> str:
        return self.e_name


If I try to show a block only if tag is equal to Tech with {% if tag == ‘Tech’%} it doesn’t work. You can’t see the block at all.
Looping through event.tags.all because one event has multiple tags

I installed, imported and mentioned taggit in my settings
It’s working fine. But I’m stuck in the above situation


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 class="d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4">
      <h5 class="text-uppercase">{{event.e_name}}</h5>
 {% endif %}
 {% endfor %}
 {% endfor %}        

The tag is an instance of the Tag model, so you should not compare it directly with string.

Answered By – Sunderam Dubey

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published