Reputation: 23
I'm trying to include the URL template tag in a string that I have created using the format_html method:
message = format_html(
'''<div>
<p>{0} wants to join {1}!</p>
{% url 'accounts:detail' pk={2} as profile_url %}
<p>
View their
<a href="{{ profile_url }}">profile</a>
</p>
<div>Approve</div>
<div>Deny</div>
</div>
''',
self.request_user.username,
self.lab.name,
self.request_user.pk)
The html that is outputted to the browser looks like this:
<a href="{ url 'accounts:detail' pk=4}">profile</a>
Basically my url tag without the %'s.
I have tried escaping the % by typing:
{%% url 'accounts:detail' pk={2} as profile_url %%}
But I receive the same output.
Any ideas?
Upvotes: 2
Views: 1866
Reputation: 538
You can directly create the URL with reverse()
:
message = format_html(
'''<div>
<p>{0} wants to join {1}!</p>
<p>
View their
<a href="{2}">profile</a>
</p>
<div>Approve</div>
<div>Deny</div>
</div>
''',
self.request_user.username,
self.lab.name,
reverse('accounts:detail', kwargs={'pk':self.request_user.pk})
)
Upvotes: 3
Reputation: 1020
You should probably try using
{{ profile_url|safe }}
or use {% autoescape %}
Autoescappe
Upvotes: 0