Django-taggit - display all tags based on the date published in the journal

Using django-taggit-templatetags2 , I can display all the tags associated with the test vlog on the template page.

I have vlogs stored in db that are not yet released to the public (displayed only after a certain date), so I can store numerous vlog data in db and then automatically issue each individual vlog on a specific day (Tuesday every week).

This means that django-taggit-templatetags2 displays all the code {% for tag in vlog_tags %} will include tags for vlog entries that are not yet displayed to the user in vlog, but are stored in db.

How can I only display tags and counters for vlog entries, where vlog_date_published not greater than now ? . There may be the same tags that are used for published and not-published journal entries. Therefore, this should be considered when displaying the tag.

Here is my model code:

 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.') .... 

Here is the code for the display template for all tags:

  {% 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 %} 

EDIT

Here is a screenshot from the database related to the tag that is displayed to the user, even if vlog is not yet displayed to the user, but is saved in db, waiting for automatic publication based on vlog_date_published > now .

In this case, the Employment NDA tag should not be displayed to the user, since the vlog record using this tag is not yet displayed to the user, since vlog_date_published larger than today (at the time of this post).

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

taggit_taggeditem table (id = 191 - FK 24 and 123): enter image description here

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

OTHER IMAGE

Thus, in this case, the next Employment NDA tag should not be displayed, since it refers to vlog data that has not yet been published / displayed to the public, while all other tags should be displayed (like vlog details that all other tags belong to publication):

enter image description here

+5
source share
2 answers

This can be achieved by creating two custom template tags to replace the {{tag}} and {{tag.num_times}} from django-taggit-templatetags2 .

The conditional user tag {% if tag.id|vlog_tag_display %} will only display the tag if the published vlog publication date is GT now and {{tag.num_times}} will be replaced with the special tag {{tag|vlog_tag_count}} , as shown below .

First add the following code to the template page. This will go through all the tags, but only display the tag if vlog is published:

 {% 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 %} 

Then add the following code to your own templates page. Here is the link to customize the page with custom template tags if this is a new experience for you:

 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
source

There are different ways to do this.

Change the default filter on the main model

 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() .... 

But not sure if this creates problems for you when you edit.

Create proxy model with default filter

 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 

In this case, you will set VlogDetails2 as the model in the settings

Change Tag Manager Source Code

The next option is to change the source code of django-taggit-templatetags2 . The code for filtering happens here

https://github.com/fizista/django-taggit-templatetags2/blob/6c456bc0071dcd9e4bc4402a15be2a8bc031da81/taggit_templatetags2/templatetags/taggit_templatetags228tags

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

You can add your filter there if you want. Not a recommended way to do though

0
source

Source: https://habr.com/ru/post/1274606/


All Articles