Login

getting the related entries with a templatetag using django-tagging

Author:
V
Posted:
February 15, 2009
Language:
HTML/template
Version:
Not specified
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. Bootstrap Accordian by Netplay4 5 years, 10 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  4. Reusable form template with generic view by roldandvg 8 years, 11 months ago
  5. Pagination Django with Boostrap by guilegarcia 9 years, 1 month ago

Comments

Please login first before commenting.