Django Quill Editor Display Saved Field

Viewed 2222

It's probably really simple, but can't figure out how to do that...

I have one really simple model:

from django.db import models
from django_quill.fields import QuillField

class Race(models.Model):
    name = models.CharField(max_length=50, unique=True)
    description = QuillField()

In admin.py

from races.models import Race

@admin.register(Race)
class RaceAdmin(admin.ModelAdmin):
    pass

So, thru the admin panel, I can use Quill to write text with HTML and add image. Great !

It is saved in the database in Quill delta format.

Now, if I want to display that description field in a template, as html... How Am I suppose to do?

Thank you very much !

2 Answers

The html of the description field can be accessed like this (try in django shell):

Race.objects.all()[0].html

You can for example render all races in an html with the code below. Note you need to use the 'safe' argument in jinja to have the variable interpreted as html code instead of text.

In the views.py:

def show_races(request):
    races_all = Race.objects.all()
    return render(request, "races.html", {"races_all": races_all})

In your races.html:

{% for race in races_all %}
  <h1> {{ race.name }} </h1>
  {{ race.description.html|safe }}
{% endfor %}

In your template file try

{{ race.description.html | safe }}
Related