Search code examples
htmldjangotemplatesraw

How to show rendered template as raw source code block?


I want to provide a rendered HTML block that shows an email signature - what it looks like with user data from the context - and also displays the populated HTML code as raw src to be copied for pasting into customers email signature. What would be the best (dry) way to do this?

I have tried render_to_string as a variable in the context but the HTML is rendered.


Solution

  • I solved this by following this answer:

    How do I perform HTML decoding/encoding using Python/Django?

    The rendered django template HTML needs to be decoded before it can be shown in the pre tag otherwise it will be marked up by the browser.