Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between rendering {{ var }} and {{ var|safe }} in Django

Say var is a variable passed with return from a view to a template. Could you please give me a simple example to understand the difference between rendering {{ var }} on the one hand and {{ var|safe }} on the other? I was not able to fully understand what is going on in the documentation: 1, 2. In which case |safe protects me? Thank you very much!

like image 673
pebox11 Avatar asked Jan 27 '26 12:01

pebox11


1 Answers

The safe template filter allows html tags and entities in the content of var. Without it, your html will be escaped so you'll see things like

<div> &nbsp;

etc on your page.

like image 126
Greg Avatar answered Jan 30 '26 04:01

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!