开发者

django: hyperlink in dict to html templates as hyperlink

I have dict with hyderlink, example :

data = [{a:<\a href="http://someexample.com/a">a</a>,
        b:'<\a href="http://someexample.com/b">b</a>'}]

Note : here i have add /a href because stack overflow takes it has hyperlink

if i want to output this in html , it displays a normal html text instead of hyperlink

template

<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a }}</td>
<td>{{ fetch.b }}</td>
</tr>
{% endfor %}
</table>

it gives output like html text instead of hyperlink

  1. <\a href="http://someexampl开发者_Python百科e.com/a">a
  2. <\a href="http://someexample.com/b">b

any help it really appreciate it.


Instead of storing the entire anchor tag, you should be storing just the URL (using URLField if you're storing it in a model), and then include it in your template as follows:

<table>
    {% for fetch in data %}
    <tr>
        <td><a href="{{ fetch.a }}">{{ fetch.a }}</a></td>
        <td><a href="{{ fetch.b }}">{{ fetch.b }}</a></td>
    </tr>
    {% endfor %}
</table>


This is happening because of automatic string escaping in the template engine. You can prevent escaping with the safe filter, like:

<table>
{% for fetch in data %}
<tr>
<td>{{ fetch.a|safe }}</td>
<td>{{ fetch.b|safe }}</td>
</tr>
{% endfor %}
</table>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜