簡體   English   中英

django-taggit - 根據發布的日期vlog顯示所有標簽

[英]django-taggit - display all tags based on date vlog published

使用django-taggit-templatetags2 ,我可以在模板頁面中顯示與測試vlog關聯的所有標簽。

我存儲在數據庫中的vlogs尚未發布給公眾(僅在特定日期之后顯示),因此我可以在數據庫中存儲大量的vlog詳細信息,然后在某一天自動釋放每個單獨的視頻博客(例如星期二)每周)。

這意味着django-taggit-templatetags2顯示{% for tag in vlog_tags %}所有代碼將包含vlog條目的標簽,這些條目尚未在vlog中顯示給用戶但存儲在db中。

如何僅顯示vlog_date_published不大於now vlog條目的標記和計數? 可能存在用於已發布和尚未發布的vlog條目的相同標記。 所以這應該考慮到標簽的顯示。

這是我的模型代碼:

from taggit.managers import TaggableManager

class VlogDetails(models.Model):
    ....
    vlog_date_published = models.DateField(null=False, blank=False, default=datetime.now, help_text='The date the vlog video will be made public.')
    vlog_tags = TaggableManager(blank=True, help_text='To make a new tag, add a comma after the new tag name.')
    ....

這是顯示所有標簽模板代碼:

    {% load taggit_templatetags2_tags %}

    {% get_taglist as vlog_tags %}

    {% for tag in vlog_tags %}
        {% if tag.num_times > 0 %}
            <a class='u-tags-v1 g-color-grey g-bg-grey-opacity-0_1 g-bg-grey--hover g-color-white--hover g-rounded-50 g-py-4 g-px-15' href="{% url 'vlog_tag' tag %}" hreflang="en" rel="tooltip" title="{% blocktrans %}Display all vlog entries containing this tag.{% endblocktrans %}"><i class="fa fa-tag icon_padding"></i> {{tag}} x {{tag.num_times}}</a>
        {% endif %}
    {% endfor %}

編輯

以下是數據庫中的屏幕截圖,指向顯示給用戶的標記,即使vlog尚未顯示給用戶,但存儲在db中,等待基於vlog_date_published > now自動發布。

在這種情況下,不應向用戶顯示Employment NDA標記,因為使用該標記的vlog條目尚未顯示給用戶,因為vlog_date_published大於今天(在本文發布時)。

vlogdetails表(id = 24): 在此輸入圖像描述

taggit_taggeditem表(id = 191 - FK的24和123): 在此輸入圖像描述

taggit_tag表(id = 123): 在此輸入圖像描述

另一個編輯

因此,在這種情況下,不應顯示以下Employment NDA標記,因為它屬於尚未向公眾發布/顯示的vlog詳細信息,而應顯示所有其他標記(因為所有其他標記都顯示為其他標簽屬於已發布):

在此輸入圖像描述

這可以通過創建兩個自定義模板標記來替換django-taggit-templatetags2中{{tag}}{{tag.num_times}}值來實現

的條件自定義標記{% if tag.id|vlog_tag_display %}如果相關的vlog出版日期是GT將僅顯示標簽now{{tag.num_times}}將通過的自定義標簽更換{{tag|vlog_tag_count}}如下所示。

首先將以下代碼添加到模板頁面。 這將循環遍歷所有標記,但僅在vlog已發布時顯示標記:

{% load customised_template_tags i18n taggit_templatetags2_tags %}
{% get_taglist as tags %}

{% for tag in tags %}
    {% if tag.num_times > 0 %}
        {# only display the tag if the vlog is published #}
        {% if tag.id|vlog_tag_display %}
                <a class='u-tags-v1 g-color-grey g-bg-grey-opacity-0_1 g-bg-grey--hover g-color-white--hover g-rounded-50 g-py-4 g-px-15' href="{% url 'vlog_tag' tag %}" hreflang="en" rel="tooltip" title="{% blocktrans %}Display all vlog entries containing this tag.{% endblocktrans %}"><i class="fa fa-tag icon_padding"></i> {{tag}} x {{tag|vlog_tag_count}}</a>
        {% endif %}
    {% endif %}
{% empty %}
    {# No Tags recorded. #}
    <br /><b>{% trans "No Tags Recorded." %}</b><br />
{% endfor %}

接下來,將以下代碼添加到自定義模板標記頁面。 以下是設置自定義模板代碼頁的鏈接 ,如果這是您的新體驗:

from zoodal.core.models import VlogDetails

@register.filter(name='vlog_tag_count')
def vlog_tag_count(value):
    date_now = timezone.now()
    count_vlog_tag = VlogDetails.objects.filter(vlog_date_published__lte=date_now, tags__name=value).count()
    """ Only count the tag if the vlog entry is published. """
    return count_vlog_tag


@register.filter(name='vlog_tag_display')
def vlog_tag_display(value):
    date_now = timezone.now()
    display_vlog_tag = VlogDetails.objects.filter(vlog_date_published__lte=date_now, tags__id=value)
    """ Only display the tag if the vlog entry is published. """
    if display_vlog_tag:
        return True
    else:
        return False

有不同的方法可以做到這一點。

更改主模型上的默認過濾器

class VlogDetailsManager(models.Manager):
    def get_queryset(self):
        return super(VlogDetailsManager, self).get_queryset().filter(vlog_date_published__lte=datetime.now())

class VlogDetails(models.Model):
    ....
    vlog_date_published = models.DateField(null=False, blank=False, default=datetime.now, help_text='The date the vlog video will be made public.')
    vlog_tags = TaggableManager(blank=True, help_text='To make a new tag, add a comma after the new tag name.')
    objects = VlogDetailsManager()
    ....

但不確定在編輯時這是否會給您帶來問題。

使用默認篩選器創建代理模型

class VlogDetailsManager(models.Manager):
    def get_queryset(self):
        return super(VlogDetailsManager, self).get_queryset().filter(vlog_date_published__lte=datetime.now())

class VlogDetails(models.Model):
    ....
    vlog_date_published = models.DateField(null=False, blank=False, default=datetime.now, help_text='The date the vlog video will be made public.')
    vlog_tags = TaggableManager(blank=True, help_text='To make a new tag, add a comma after the new tag name.')
    ....

class VlogDetails2(VlogDetails):
      objects = VlogDetailsManager()
      class Meta:
            proxy = True

在這種情況下,您將在設置中將VlogDetails2設置為模型

更改標簽管理器的源代碼

下一步是更改django-taggit-templatetags2的源代碼。 過濾代碼在這里發生

https://github.com/fizista/django-taggit-templatetags2/blob/6c456bc0071dcd9e4bc4402a15be2a8bc031da81/taggit_templatetags2/templatetags/taggit_templatetags2_tags.py#L28

https://github.com/fizista/django-taggit-templatetags2/blob/6c456bc0071dcd9e4bc4402a15be2a8bc031da81/taggit_templatetags2/templatetags/taggit_templatetags2_tags.py#L44

如果需要,您可以在那里添加過濾器。 雖然不是推薦的做法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM