无法访问保存在 Django 模板上的 Quill 表单字段中的文本

Can't access text saved in a Quill form field on Django template

在我的 django 模板中,我想访问我的 Creator class 实例的生物属性。此生物在 Creator 模型 class 中设置为 QuillField。当我尝试访问 creator.bio 时,呈现给页面的所有内容如下:

我想要的是我在表格中输入并保存的格式化文本(即简历)的实际段落。截至目前,QuillField 只能通过 Django 管理页面中的表单访问。该问题与 Quill UI 无关,而是能够访问我在该表单字段中写入的文本并以可读格式将其呈现到页面。

来自 models.py:

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

class Creator(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=100, default='Creator')
    bio = QuillField()
    photo = models.ImageField(upload_to='images/', default='static/assets/icons/user-solid.svg')
    email = models.EmailField(max_length=100)
    website = models.URLField(max_length=1000, blank=True)
    facebook = models.URLField(max_length=1000, blank=True)
    twitter = models.URLField(max_length=1000, blank=True)
    instagram = models.URLField(max_length=1000, blank=True)

    def __str__(self):
        return self.name

在views.py中:

def about(request):
    context = {"creators" : Creator.objects.all()}
    return render(request, 'about.html', context)

并且,在模板中:

        <section id="creator-container">
            {% for creator in creators %}
                <div class="creator-square">
                    <h4>{{ creator.name }}</h4>
                    <h5>{{ creator.title }}</h5>
                    <img src="../../media/{{ creator.photo }}" alt="{{actor.name}} headshot" id="creator-photo">
                    <p class="creator-bio">{{ creator.bio }}</p>
                </div>
            {% endfor %}
        </section>

如果我将 creator.bio 对象打印到控制台,这就是我得到的:

{"delta":"{\"ops\":[{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"bold\":true},\"insert\":\"Sharon Yablon\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\" is an award-winning playwright who has been writing and directing her plays in Los Angeles for many years. Her work has appeared in a variety of sites, and on stage with The Echo Theater Company, Padua Playwrights, Zombie Joe's Underground Theater, The Lost Studio, Theater Unleashed, Bootleg, Theater of N.O.T.E., and others. Her short stories, \\"Perfidia\\" and \\"The Caller,\\" can be found in journals, and her published plays are in \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"Desert Road's One Acts of Note\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\", \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"Fever Dreams\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\", \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"Los Angeles Under the Influence\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\", \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"LA Writers and Their Works\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\", and others. She was co-editor of an anthology of plays from the LA underground scene titled \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"I Might Be The Person You Are Talking To, \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\"and most recently, her play \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"Hello Stranger\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\" (Theater of N.O.T.E., 2017) was published by Original Works. She is a frequent writer and sometime co-curator with Susan Hayden's \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"Library Girl\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\", a \\"Best of the Westside\\" monthly literary series centered around a music theme. Her one-acts inspired by crimes in LA history have appeared in \"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\",\"italic\":true},\"insert\":\"LA True Crime’s\"},{\"attributes\":{\"background\":\"transparent\",\"color\":\"#000000\"},\"insert\":\" quarterly evenings since its inception in 2015. \"},{\"insert\":\"\n\"}]}","html":"<p><strong style=\"background-color: transparent; color: rgb(0, 0, 0);\">Sharon Yablon</strong><span style=\"background-color: transparent; color: rgb(0, 0, 0);\"> is an award-winning playwright who has been writing and directing her plays in Los Angeles for many years. Her work has appeared in a variety of sites, and on stage with The Echo Theater Company, Padua Playwrights, Zombie Joe's Underground Theater, The Lost Studio, Theater Unleashed, Bootleg, Theater of N.O.T.E., and others. Her short stories, \"Perfidia\" and \"The Caller,\" can be found in journals, and her published plays are in </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">Desert Road's One Acts of Note</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\">, </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">Fever Dreams</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\">, </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">Los Angeles Under the Influence</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\">, </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">LA Writers and Their Works</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\">, and others. She was co-editor of an anthology of plays from the LA underground scene titled </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">I Might Be The Person You Are Talking To, </em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\">and most recently, her play </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">Hello Stranger</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\"> (Theater of N.O.T.E., 2017) was published by Original Works. She is a frequent writer and sometime co-curator with Susan Hayden's </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">Library Girl</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\">, a \"Best of the Westside\" monthly literary series centered around a music theme. Her one-acts inspired by crimes in LA history have appeared in </span><em style=\"background-color: transparent; color: rgb(0, 0, 0);\">LA True Crime’s</em><span style=\"background-color: transparent; color: rgb(0, 0, 0);\"> quarterly evenings since its inception in 2015. </span></p>"}

有谁知道如何访问它以使其正确呈现为 HTML 文本?

基于 https://github.com/LeeHanYeong/django-quill-editor/issues/12 听起来您需要使用:

{{ creator.bio.html|safe }}

(如果您不确定 HTML 是否恶意,请谨慎使用 safe!)