Generating Random text in Django Template

anuragrana

Anurag Rana

Posted on August 19, 2019

Generating Random text in Django Template

Demo: https://www.djangotemplatefiddle.com/f/wV5xSd/

We can generate random text in Django templates. This is used to fill the sample data.

Django provides inbuilt template tag {% lorem %} for this.

To generate one paragraph with lorem ipsum text, use the above tag as it
is.

<!-- Lorem ipsum paramgraph -->
{% lorem %}

Output:

<!-- Lorem ipsum paramgraph -->
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

To generate let's say 2 paragraphs with random text, use tag with parameters.

<!-- random text paragraph -->
{% lorem 2 b %}

Output:

<!-- random text paragraph -->
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Deserunt quisquam dolores minus tempore aperiam itaque minima maxime, atque aperiam libero recusandae quod aliquid sed quo a deserunt, a at rem? Incidunt aut quibusdam est distinctio amet nemo, beatae dolorum fugit corporis recusandae dolorem praesentium vel obcaecati consectetur, voluptas quisquam a neque atque debitis, tenetur eaque nostrum ex?

To generate a few random words, use the tag with below parameters:

<!-- random words of length 10 -->
{% lorem 10 w random %}

Output:

<!-- random words of length 10 -->
autem veritatis quisquam optio quibusdam non qui assumenda dolores alias

You can see (and experiment) the demo here: https://www.djangotemplatefiddle.com/f/wV5xSd/

💖 💪 🙅 🚩
anuragrana
Anurag Rana

Posted on August 19, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related