0

I've wrote a custom view to parse markdown files using regex and I'm passing content as a string literal to the template

context = "Django is a web framework written using <a href='https://www.python.org/'>Python</a>"

return render(request, "blog/post.html", {"context": context})

And in template:

<p>{{ context }}</p>

But the engine renders content as a plain text. How to make links to be links and paragraphs to be paragraphs?

3
  • You need to mark it as safe because you trust the markup in it; {{ context|safe }}. Alternatively you can use mark_safe() in the view to pass a safe string in the context; docs.djangoproject.com/en/3.2/ref/utils/… Commented Dec 26, 2021 at 0:14
  • It works. Thanks. But I have several other variables that work without safe filter. Why do these variables work without being marked as safe? Commented Dec 26, 2021 at 9:27
  • I'd assume the other variables don't contain anything that django considers potentially dangerous. Commented Dec 27, 2021 at 23:47

1 Answer 1

3

Just add safe template tag like @markwalker_ said in comments it will turn off autoescape of html tags.

 <p>{{ context|safe }}</p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.