dyl*_*sss 5 python django quill
在我的 django 模板中,我想访问 Creator 类实例的 bio 属性。该简介在 Creator 模型类中设置为 QuillField。当我尝试访问 Creator.bio 时,呈现到页面的所有内容如下:
\n<django_quill.fields.FieldQuill 对象位于 0x1084ce518>
\n我想要的是我在表单中输入并保存的格式化文本的实际段落(即个人简介)。截至目前,QuillField 只能通过 Django 管理页面中的表单访问。该问题与 Quill UI 无关,而是能够访问我写入该表单字段的文本并将其以可读格式呈现到页面。
\n来自 models.py:
\nfrom django.db import models\nfrom django_quill.fields import QuillField\n\nclass Creator(models.Model):\n name = models.CharField(max_length=100)\n title = models.CharField(max_length=100, default='Creator')\n bio = QuillField()\n photo = models.ImageField(upload_to='images/', default='static/assets/icons/user-solid.svg')\n email = models.EmailField(max_length=100)\n website = models.URLField(max_length=1000, blank=True)\n facebook = models.URLField(max_length=1000, blank=True)\n twitter = models.URLField(max_length=1000, blank=True)\n instagram = models.URLField(max_length=1000, blank=True)\n\n def __str__(self):\n return self.name\n
Run Code Online (Sandbox Code Playgroud)\n在views.py中:
\ndef about(request):\n context = {"creators" : Creator.objects.all()}\n return render(request, 'about.html', context)\n
Run Code Online (Sandbox Code Playgroud)\n并且,在模板中:
\n <section id="creator-container">\n {% for creator in creators %}\n <div class="creator-square">\n <h4>{{ creator.name }}</h4>\n <h5>{{ creator.title }}</h5>\n <img src="../../media/{{ creator.photo }}" alt="{{actor.name}} headshot" id="creator-photo">\n <p class="creator-bio">{{ creator.bio }}</p>\n </div>\n {% endfor %}\n </section>\n
Run Code Online (Sandbox Code Playgroud)\n如果我将 Creator.bio 对象打印到控制台,这就是我得到的:
\n{"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\xe2\x80\x99s\\"},{\\"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\xe2\x80\x99s</em><span style=\\"background-color: transparent; color: rgb(0, 0, 0);\\"> quarterly evenings since its inception in 2015. </span></p>"}\n
Run Code Online (Sandbox Code Playgroud)\n有谁知道如何访问它以便它正确呈现为 HTML 文本?
\n基于https://github.com/LeeHanYeong/django-quill-editor/issues/12听起来您需要使用:
{{ creator.bio.html|safe }}
Run Code Online (Sandbox Code Playgroud)
safe
(不过,如果您不确定 HTML 是否是恶意的,请谨慎使用!)
归档时间: |
|
查看次数: |
1644 次 |
最近记录: |