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

时间:2018-01-10 00:51:48

标签: python django django-taggit

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

我的数据库中存储的vlog尚未发布给公众(仅在特定日期之后显示),因此我可以在数据库中存储大量的vlog详细信息,然后在某一天自动释放每个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尚未显示给用户,但存储在数据库中,等待基于{{1自动发布}&gt; vlog_date_published

在这种情况下,now的标记不应显示给用户,因为使用该标记的vlog条目尚未显示给用户,因为Employment NDA大于今天(在这篇文章的时候)。

vlogdetails表(id = 24):enter image description here

taggit_taggeditem表(id = 191 - FK&#39;&amp; 123):enter image description here

taggit_tag表(id = 123): enter image description here

另一个编辑

因此,在这种情况下,不应显示以下vlog_date_published标记,因为它属于尚未向公众发布/显示的vlog详细信息,而应显示所有其他标记(如所有其他标签所属的vlog详细信息已发布):

enter image description here

2 个答案:

答案 0 :(得分:1)

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

如果相关的视频博客发布日期为GT {% if tag.id|vlog_tag_display %},则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 %}

接下来,将以下代码添加到自定义模板标记页面。如果这是您的新体验,请设置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

答案 1 :(得分:0)

有不同的方法可以做到这一点。

更改主模型上的默认过滤器

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

如果需要,您可以在那里添加过滤器。虽然不是推荐的做法