0

I have a list being passed to my template. However I want to 3rd item to be href`d. If i add the html in the view and pass it to the template it get escaped and displayed as html rather then a link.

Whats the best way to achieve this ?

Thanks,

2 Answers 2

1

Use the forloop.counter in your template to conditionally add the href rather than trying to add HTML in the view:

template.html

<ul>
{% for item in mylist %}
    <li>
        {% if forloop.counter == 3 %}<a href="myhref">{% endif %}{{ item }}{% if forloop.counter == 3 %}</a>{% endif %}
    </li>
{% endfor %}
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use the autoscape tag or the safe filter, see the docs here and here.

Take in mind that can be dangerous to allow rendering of HTML directly if the data come from untrusted source.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.