pythonjinja2cookiecutter

Generate random letters using Jinja for cookiecutter template


I am looking to generate a random 8 letter hash for a cookeicutter template. In pure Python, this would generate what I'd like:

In [3]: import random
In [3]: import string
In [4]: ''.join(random.choice(string.ascii_lowercase) for i in range(8))                                                                                        
Out[4]: 'jrqcwtav'

In Jinja, the closest thing I could find would be a multiline solution. I need a one-liner. Any ideas?


Solution

  • If you're using cookiecutter>=1.7, you can use filter random_ascii_string like this :

    {{ random_ascii_string(8) }}
    

    More information available about random_ascii_string.