【问题标题】:Can't access text saved in a Quill form field on Django template无法访问保存在 Django 模板上的 Quill 表单字段中的文本
【发布时间】:2021-02-01 22:10:25
【问题描述】:

在我的 django 模板中,我想访问我的 Creator 类实例的 bio 属性。此简历在 Creator 模型类中设置为 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 文本?

【问题讨论】:

  • 你使用的是什么版本的 django?该软件包看起来确实过时了,因此使用起来并不明智。至于它保存的数据,它看起来像是转义了 JSON
  • @markwalker_ 我正在使用 Django 3.1.5。这是 Quill 包:pypi.org/project/django-quill-editor

标签: python django quill


【解决方案1】:

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

{{ creator.bio.html|safe }}

(如果您不确定 HTML 不是恶意的,请小心使用 safe!)

【讨论】:

  • 你才是真正的MVP!!谢谢一百万!
猜你喜欢
  • 1970-01-01
  • 2012-06-05
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 2014-07-21
相关资源
最近更新 更多