[Answer]-How to random a file from a dir within static dir?


import random, glob
path = random.choice(glob.glob('~/app/static/img/funny/*.*'))

Now in the path variable you’ll have a path of a random file from img/funny

You might want to do the following if you want the path to be relative to ~/app/static/

path = path.replace("~/app/static/","")

And then you can use it in your templates like:

<img src="{{ path }}"/>

Leave a comment