'Django Quill Editor Display Saved Field
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 !
Solution 1:[1]
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 %}
Solution 2:[2]
Will .html|safe
contain the quilljs css such as .ql-editor
or .ql-indent
. If not how can i render the exact html that was written.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Simi |
Solution 2 | Afrid Syed |