【问题标题】:django-taggit - display all tags based on date vlog publisheddjango-taggit - 根据 vlog 发布日期显示所有标签
【发布时间】:2018-06-19 02:45:59
【问题描述】:

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

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

这意味着django-taggit-templatetags2显示{% for tag in vlog_tags %}的所有代码将包含尚未在vlog中向用户显示但存储在db中的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 大于今天(在这篇文章)。

vlogdetails 表(id​​ = 24):

taggit_taggeditem 表(id​​=191 - FK 的 24 和 123):

taggit_tag 表(id​​=123):

另一个编辑

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

【问题讨论】:

标签: python django django-taggit


【解决方案1】:

你可以通过不同的方式来做到这一点。

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

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

如果需要,您可以在此处添加过滤器。虽然不推荐这样做

【讨论】:

  • 如何在设置中将VlogDetails2设置为模型?按照你的建议?
  • 除非你指的是 admin.py 设置?
  • @user1261774 我想你可能不得不使用{% get_taglist as tags for 'yourapp.yourmodel' %}
  • 谢谢。但是,您的代码不会隐藏标签,它会隐藏 django_admin 中的记录。
  • Okie 我认为这需要一些调试,你能提供一个最小的 git 项目给我一些基础数据以及预期和实际输出的差异吗?
【解决方案2】:

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

{% if tag.id|vlog_tag_display %}的条件自定义标签只有在相关vlog发布日期为GTnow时才会显示标签,{{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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2018-06-13
    • 2012-10-05
    • 2017-10-14
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多