I've taken text from my models and used markdown python extension to turn it into html... but it renders as html as a string on my site like this:
bot:
<p><code>62%</code> Not quite. You got this! Appreciate your efforts</p>
kaik:
<p>ss</p>
Rather than showing the html as a string, I'd like it to format it as html.
My views.py currently:
from django.shortcuts import render
from .models import ChatStream
from django.http import HttpResponseRedirect
import random
import markdown
from django.template import RequestContext
def send(request):
message = request.POST.get('userMessage', False)
ip = visitor_ip_address(request)
response = routes(message, 'website_' + str(ip))
chatItem = ChatStream(ss= markdown.markdown(response), user= markdown.markdown(message), name=ip)
chatItem.save()
return HttpResponseRedirect('/chattr/')
I'm using markdown to convert the text in my models ChatStream into nicely website formattable text, but rather than formatting as html it just prints the html as a string onto the site.
my chattr.html:
{% for chat_stream in chat %}
<p>
{% if forloop.last %}
{% else %}
<b>bot:</b> <br> {{chat_stream.ss}} <br>
<b>{{user}}:</b> <br> {{chat_stream.user}} <br>
{% endif %}
</p>
{% endfor %}
It should look like this in the website:
bot:
62%
Not quite. You got this! Appreciate your efforts
kaik:
ss
Django's template engine will escape the text, such that if the text contains a < b, it will render it as a < b. You can disable this escaping with the |safe template filter [Django-doc]:
{% for chat_stream in chat %}
<p>
{% if forloop.last %}
{% else %}
<b>bot:</b> <br> {{ chat_stream.ss|safe }} <br>
<b>{{ user }}:</b> <br> {{ chat_stream.user }} <br>
{% endif %}
</p>
{% endfor %}