Is it possible to store a django template tag value, like lorem, in a variable?

264 views Asked by At

I came across a scenario where I wanted to display a fake name on a django 3.1 template. So I used the following code:

Name: {% lorem 2 w random %}

Then I realized I had another location on my display that would contain the same name. Of course if I use the same code again, then I would get a random different two words.

So I tried to store the value in a variable so I could have the exact same random name twice. I am looking over a number of records and wanted random names for each record, so although this doesn't work, it is what I was trying to attempt.

{% for d in dict %}
    {% with lorem 2 w random as randName %}
        Name: {{ randName }}<br>
        ....<br>
        Name: {{ randName }}<br><br>
    {% endwith %}
{% endfor %}

Expected Output:

Name: TEMPORIBUS EXERCITATIONEM
....
Name: TEMPORIBUS EXERCITATIONEM

Name: QUAM PARIATUR
....
Name: QUAM PARIATUR

for as many dictionary items exist. This is not a huge deal, more curiosity and wanting to better understand the django framework. Is it possible to get a value from the lorem template tag and store it as a variable?

I have also tried creating a custom template tag:

@register.simple_tag
def setvar(val=None):
  return val

And on my template:

{% setvar randName as lorem 2 w random %}

but I get the error that it only expects one parameter.

0

There are 0 answers