Join, with link, template filter
A custom filter allows you to create a linked item using a join.
Save the following in a file such as appname/templatetags/custom_tags.py.
from django import template
register = template.Library()
@register.filter('join_link')
def join_link(value, arg):
from django.utils.html import conditional_escape
arr = []
for i in value:
arr.append('<a href="%s">%s</a>' % (
i.get_absolute_url(), conditional_escape(i)
))
return arg.join(arr)In your template add the following, assuming posts is a Queryset from Post.objects.all() or something.
{% load custom_tags %}
{{ posts|join_link:', '|safe }}This would produce something like (line-break for formatting on here only)
<a href="/news/hello-world.html">Hello World</a>,
<a href="/news/goodbye-world.html">Goodbye world</a>This example does use get_absolute_url as well as automatically using the __str__ or __unicode__ methods, so these will need to be defined in your model.