Huy Than
Huy Than

Reputation: 1556

Use {% with %} tag in Django HTML template

I have a list of areas like this :

areas  = ['Anaheim', 'Westminster', 'Brea'...]

I would like to display them in HTML as:

<option value="Anaheim(Orange)">Anaheim</option>
<option value="Westminster(Orange)">Westminster</option>
<option value="Brea(Orange)">Brea</option>

So I try this:

{%for area in areas%}
    {% with area|add:"(Orange)" as area_county%}
        <option value="{{area_county}}">{{area}}</option>
    {% endwith %}
{%endfor%}

But the output is this:

<option value="">Anaheim</option>
<option value="">Westminster</option>
<option value="">Brea</option>

Where did I do wrong?

Thank you!

Upvotes: 1

Views: 103

Answers (2)

Durante
Durante

Reputation: 305

Maybe if you try only put the text next to the template variable like:

{%for area in areas%}
    <option value="{{area}}(Orange)">{{area}}</option>
{%endfor%}

I don't know why you try but is the same for me.

Upvotes: 2

jrbedard
jrbedard

Reputation: 3730

You could use extends instead of with:

{% extends area|add:"(orange)" %} 

Upvotes: 1

Related Questions