使用django-taggit-templatetags2 2,我可以在模板页面中显示与测试vlog相关的所有标记。
我将vlog存储在db中,这些日志尚未向公众发布(只在某个日期之后显示),这样我就可以在db中存储大量的vlog详细信息,然后在特定的一天(例如每周的星期二)自动释放每个vlog。
这意味着django-taggit-templatetags2 2显示{% for tag in vlog_tags %}的所有代码将包括尚未在vlog中向用户显示但存储在db中的vlog条目的标记。
如何才能只显示vlog条目的标记和计数,如果vlog_date_published now**?** 不大于,则可能存在用于已发布和尚未发布的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大于今天(在本文发布时)。
Vlog细目表(id = 24):

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

taggit_tag表(id=123):

另一个编辑
因此,在此场景中,不应显示Employment NDA的以下标记,因为它属于尚未向公众发布/显示的vlog详细信息,而所有其他标记都应显示(因为所有其他标记都已发布):

发布于 2018-02-21 21:20:48
这可以通过创建两个自定义模板标记来替换{{tag}}和来自django-taggit-templatetags2 2的{{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发布于 2018-02-18 07:29:57
你可以用不同的方法去做。
更改主模型上的默认筛选器
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://stackoverflow.com/questions/48178814
复制相似问题