Ulmer
Ulmer

Reputation: 1580

How can I render template tags from a view in Django

I need to escape just part user input for output. For example

Hey a status update for <a href="/profile/2353">@Friend Name</a> 

I just need to escape the link part of text in case someone does something like

<h1>HaCkEDED!!!!</h1>

So I can't escape the entire block. So I tried this

'{%% autoescape off %%} <a href="/profile/%s">%s</a>{%% endautoescape %%}' % (tag, at)

When I send this to output in the browser here is what I get

 {% autoescape off %} <a href="/profile/2353">@Friend Name</a>{% endautoescape %}

How can I get the autoescape to work? I can't use a separate variable and put

{{ tag | safe }}

because one post might have multiple tags and the tags will be in different spots. So how can I get Django to let me output one of its templates?

Upvotes: 1

Views: 465

Answers (2)

machaku
machaku

Reputation: 1196

I think it will be easier to do that using a custom templatetag or filter, probably with some regex which can identify the tags.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 600026

I don't understand exactly what you're trying to do, but in a view you can mark items as not needing autoescaping by using django.utils.safestring.mark_safe.

Upvotes: 3

Related Questions