'Random string in Django template
Is there any way to have a random string in a Django template?
I would like to have multiple strings displaying randomly like:
{% here generate random number rnd ?%}
{% if rnd == 1 %}
{% trans "hello my name is john" %}
{% endif %}
{% if rnd == 2 %}
{% trans "hello my name is bill" %}
{% endif %}
EDIT:
Thanks for answer but my case needed something more specific as it was in the base template (which I forgot to mention sorry ). So after crawling Google and some docs I fall on context processor article which did the job, I found it a little bit "heavy" anyway just for generating a random number...
here is the blog page : http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
Template tag did not the trick (or I did not find how) as it return a tag that cannot be translated as I remember (see blocktrans doc)
I did not find a way to generate a number for the base view (is there any?) and if there is a way better than context process I'd be glad to have some info.
Solution 1:[1]
Instead of using if-else blocks, passing a list of strings to your template and using random filter seems better
In your view:
my_strings = ['string1', 'string2', ...]
...
return render_to_response('some.html', {'my_strings':my_strings})
And in your template:
{{ my_strings|random }}
Solution 2:[2]
You could do something like that:
{# set either "1" or "2" to rnd, "12"|make_list outputs the list [u"1", u"2"] #}
{# and random chooses one item randomly out of this list #}
{% with rnd="12"|make_list|random %}
{% if rnd == "1" %}
{% trans "hello my name is john" %}
{% elif rnd == "2" %}
{% trans "hello my name is bill" %}
{% endif %}
{% endwith %}
Look at the "Built-in template tags and filters" documentation for more info: https://docs.djangoproject.com/en/1.4/ref/templates/builtins/
Solution 3:[3]
You should write a custom template tag, see this one (with close functionality) as an example: http://djangosnippets.org/snippets/150/, but if it is not critical, to array of lines in the template, I would rather generete this random string in view.
Solution 4:[4]
In case you want to include random template and have it available globally :
in context_processors:
def sample(request):
my_strings = ['string1', 'string2', ...]
return {banners: my_stirngs}
in tempale (giving that your includes are in 'inc' folder ):
{% with banners|random as template %}
{% include 'inc/'|add:template %}
{% endwith %}
Solution 5:[5]
In a template:
{% random_number as rnd %}
The best 6 digits (by default) random number is: {{ rnd }}
{% random_number 9 as rnd9 %}
The best 9 digit random number is: {{ rnd9 }}
In markup.py:
@register.assignment_tag()
def random_number(length=6):
from random import randint
return randint(10**(length-1), (10**(length)-1))
Taken from https://djangosnippets.org/snippets/2984/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | dbf |
| Solution 4 | zzart |
| Solution 5 | oriadam |
