print html tag programmatically on GAE Python
-
07-03-2021 - |
Question
is there any way to print html code on GAE webapp?
for k, v in menubar.iteritems():
if(re.search(v, self.request.path)):
menustring += '<li class="current"><a href="'+v+'">'+k+'</a></li>'
else:
menustring += '<li><a href="'+v+'">'+k+'</a></li>'
print on template:
<ul>
{{ menustring }}
</ul>
it shows < > instead of < >
Solution
HTML tags (or rather, <
) are automatically escaped by Django templates to prevent cross site scripting vulnerabilities. You can use the safe
filter (eg {{menustring|safe}}
) to prevent escaping.
You almost certainly shouldn't be using this here, though - you should instead pass in the list of groups into the template, and iterate over them there. This ensures that individual items are escaped where required, and also avoids putting template rendering logic into your code.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow