django-taggit - 根据 vlog 发布日期显示所有标签

django-taggit - display all tags based on date vlog published

使用django-taggit-templatetags2,我可以在模板页面中显示与测试视频日志关联的所有标签。

我在数据库中存储了尚未发布到 public 的 vlog(仅在特定日期后显示),因此我可以在数据库中存储大量 vlog 详细信息,然后自动释放每个人vlog 在某一天(比如每周的星期二)。

这意味着 django-taggit-templatetags2 显示 {% for tag in vlog_tags %} 的所有代码将包括尚未在 vlog 中向用户显示但存储在数据库中的 vlog 条目的标签。

如何只显示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 尚未显示给用户,但存储在数据库中等待根据 vlog_date_published > now.

在这种情况下,不应向用户显示 Employment NDA 的标签,因为使用该标签的 vlog 条目尚未向用户显示,因为 vlog_date_published 大于今天(当时 post)。

vlogdetails table (id = 24):

taggit_taggeditem table (id=191 - FK's 24 & 123):

taggit_tag table (id=123):

另一个编辑

所以在这种情况下,Employment NDA 的以下标签不应显示,因为它属于尚未发布/显示给 public 的 vlog 详细信息,而其他所有应显示标签(因为所有其他标签所属的 vlog 详细信息已发布):

您可以采用不同的方法。

更改主模型的默认过滤器

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

如果需要,您可以在那里添加过滤器。不过不推荐这样做

这可以通过创建两个自定义模板标签来替换 {{tag}}django-taggit-templatetags2 中的 {{tag.num_times}} 值来实现。

{% if tag.id|vlog_tag_display %}的条件自定义标签仅在相关vlog发布日期为GT时显示标签now{{tag.num_times}}将被[=17的自定义标签替换=]如下图。

首先将以下代码添加到您的模板页面。这将遍历所有标签,但仅在 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 %}

接下来将以下代码添加到您的自定义模板标签页面。如果这对您来说是新体验,这里是 link 设置自定义模板标签页面:

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