dannyxn dannyxn - 1 month ago 10
HTML Question

How to perform html markups on site in Django

Recently I've started to learn Django, I've decided to make
a sample blog website. I've made Post model which creates and publish post. But there's a problem, I've no idea how to attach html markups to my Post object's attribute for instance "text" e.g
I want to bold my text, but instead text, I see "

<b>text</b>
".
Here is how I've made Post model:

from django.db import models
from django.utils import timezone

class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
introduction = models.TextField()
text = models.TextField()
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)

def publish(self):
self.published_date = timezone.now()
self.save()

def __str__(self):
return self.title

Answer

On your template file use the safe filter like this:

<h1>{{post.title | safe}}</h1>