[Answered ]-Django forms – display HTML <pre> code snippets within rendered {{ form.text }}

1πŸ‘

βœ…

If you want to keep it simple and short, you could add the safe templatetag to your body. This will allow you to render HTML from your input:
https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#safe

...
<div class='card-body' style='background: #1a1a1a;'>
    <p>{{ article.body|safe|linebreaks }}</p>
    ...
</div>
...

And then, when you’re writing the blog, you can wrap the code inside a <pre> tag:

Hello this is my tutorial
This is a piece of code:
<pre>my_function()</pre>
Hope you enjoyed it!

And that will be rendered.

Leave a comment