as part of a keyword cloud function in Django, I am trying to output a list of strings. Is there a filter for templates which allows you to shuffle items in a list? I thought this would be straightforward, but I can't find any applicable filters in the official docs.
it's straightforward to make yours.
# app/templatetags/shuffle.py
import random
from django import template
register = template.Library()
@register.filter
def shuffle(arg):
tmp = list(arg)[:]
random.shuffle(tmp)
return tmp
and then in your template:
{% load shuffle %}
<ul>
{% for item in list|shuffle %}
<li>{{ item }}</li>
{% endfor %}
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With