Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

python 2.7 - Generate random number with jinja2

I need to generate a random number between 1 and 50. Aparently the random filter needs a sequence. How can I create a list with numbers from 1 to 50 in Jinja?

{{ [1,n,50]|random() }}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Jinja2 also includes the range function which returns a sequence of numbers from start to end - 1, so you can use it with random:

Your lucky number is: {{ range(1, 51) | random }}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...