Login

getting the related entries with a templatetag using django-tagging

Author:
V
Posted:
February 15, 2009
Language:
HTML/template
Version:
Not specified
Tags:
templatetag tagging related django-tagging
Score:
0 (after 0 ratings)

Django tagging by default doesn't provide a templatetag to get the related objects for another object. Even though this is implemented as a model. Still, one can use the existing templatetags to achieve the same outcome.

Of course, writing a custom templatetag would be more efficient in terms of database access.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{% block related %}
<div id="related-posts"><h2>{% trans "Related posts" %}</h2>
{% for tag in object.get_tags %}
	{% tagged_objects tag in blog.Post as relatedItems %}
	{% if relatedItems|length_is:"0" %}
	{% else %}
	<h3>{{ tag }}</h3>
	<ul>
	{% for relatedItem in relatedItems|slice:":3" %}
	<li><a href="{{ relatedItem.get_absolute_url }}">{{ relatedItem.name|typogrify }}</a></li>
	{% endfor %}
	</ul>
	{% endif %}
{% endfor %}
</div>
{% endblock %}

More like this

  1. Filter on Multiple M2M Objects Simultaneously by axiak 8 years, 4 months ago
  2. Load templatetag libraries via settings by miracle2k 8 years, 1 month ago
  3. Hat-trick for getting categories related to an object group by pablo_PXL 4 years, 7 months ago
  4. shuffle templatetag by deanmalmgren 4 years, 6 months ago
  5. Inspect object debugging tag by dballanc 8 years, 1 month ago

Comments

Please login first before commenting.