在Django中的标签问题 | 使用taggit存储标签

huangapple go评论77阅读模式
英文:

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:


&lt;div id=&quot;tab-2&quot; class=&quot;tab-pane fade show p-0&quot;&gt;
 {% for event in events %}
 {% for tag in event.tags.all %}
 {% if tag == &#39;Tech&#39; %}
   &lt;div class=&quot;row g-3&quot;&gt;
    &lt;div class=&quot;col mb-5&quot;&gt;
     &lt;div class=&quot;d-flex h-100&quot;&gt;
      &lt;div class=&quot;flex-shrink-0&quot;&gt;
       &lt;img class=&quot;img-fluid&quot; src=&quot;{{event.e_image.url}}&quot; alt=&quot;&quot; style=&quot;width: 430px; height: 350px;&quot;&gt;
       &lt;h4 class=&quot;bg-dark text-primary p-2 m-0&quot;&gt;{{event.e_date}}&lt;/h4&gt;
      &lt;/div&gt;
      &lt;div class=&quot;d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4&quot;&gt;
      &lt;h5 class=&quot;text-uppercase&quot;&gt;{{event.e_name}}&lt;/h5&gt;
      &lt;/div&gt;
     &lt;/div&gt;
    &lt;/div&gt;
   &lt;/div&gt;
       {% endif %}
       {% endfor %}
       {% endfor %}        
&lt;/div&gt;

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=&#39;eventimg&#39;)
    e_date = models.DateTimeField()
    e_url = models.URLField(null=True, blank=True)
    tags = TaggableManager()

    def __str__(self) -&gt; 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' %},如下所示:

&lt;div id=&quot;tab-2&quot; class=&quot;tab-pane fade show p-0&quot;&gt;
 {% for event in events %}
 {% for tag in event.tags.all %}
 {% if tag.name == 'Tech' %}
   &lt;div class=&quot;row g-3&quot;&gt;
    &lt;div class=&quot;col mb-5&quot;&gt;
     &lt;div class=&quot;d-flex h-100&quot;&gt;
      &lt;div class=&quot;flex-shrink-0&quot;&gt;
       &lt;img class=&quot;img-fluid&quot; src=&quot;{{event.e_image.url}}&quot; alt=&quot;&quot; style=&quot;width: 430px; height: 350px;&quot;&gt;
       &lt;h4 class=&quot;bg-dark text-primary p-2 m-0&quot;&gt;{{event.e_date}}&lt;/h4&gt;
      &lt;/div&gt;
      &lt;div class=&quot;d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4&quot;&gt;
      &lt;h5 class=&quot;text-uppercase&quot;&gt;{{event.e_name}}&lt;/h5&gt;
      &lt;/div&gt;
     &lt;/div&gt;
    &lt;/div&gt;
   &lt;/div&gt;
 {% endif %}
 {% endfor %}
 {% endfor %}        
&lt;/div&gt;

tagTag 模型的实例,因此您不应该直接将其与字符串进行比较。

英文:

After looking at GitHub code, there is a name field so it should be {% if tag.name == &#39;Tech&#39; %} instead, like so:

&lt;div id=&quot;tab-2&quot; class=&quot;tab-pane fade show p-0&quot;&gt;
 {% for event in events %}
 {% for tag in event.tags.all %}
 {% if tag.name == &#39;Tech&#39; %}
   &lt;div class=&quot;row g-3&quot;&gt;
    &lt;div class=&quot;col mb-5&quot;&gt;
     &lt;div class=&quot;d-flex h-100&quot;&gt;
      &lt;div class=&quot;flex-shrink-0&quot;&gt;
       &lt;img class=&quot;img-fluid&quot; src=&quot;{{event.e_image.url}}&quot; alt=&quot;&quot; style=&quot;width: 430px; height: 350px;&quot;&gt;
       &lt;h4 class=&quot;bg-dark text-primary p-2 m-0&quot;&gt;{{event.e_date}}&lt;/h4&gt;
      &lt;/div&gt;
      &lt;div class=&quot;d-flex flex-column justify-content-center text-start bg-secondary border-inner px-4&quot;&gt;
      &lt;h5 class=&quot;text-uppercase&quot;&gt;{{event.e_name}}&lt;/h5&gt;
      &lt;/div&gt;
     &lt;/div&gt;
    &lt;/div&gt;
   &lt;/div&gt;
 {% endif %}
 {% endfor %}
 {% endfor %}        
&lt;/div&gt;

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

huangapple
  • 本文由 发表于 2023年3月3日 23:25:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定