简体   繁体   English

Django:为什么我放置在 Django Summernote 中的文本在我的 Z4C4AD5FCA2E0381F74DBB1CEDAA 模板中显示 HTML 标签?

[英]Django: Why does the text that I placed in Django Summernote displays HTML tags in my HTML template?

I have installed Django Summernote by following the guide from GitHub and have successfully added it to my admin page.我已经按照 GitHub 的指南安装了 Django Summernote,并已成功将其添加到我的管理页面。 However, when I try to edit the text using summernote and view it on the HTML template, it shows the code view instead of the text only.但是,当我尝试使用 Summernote 编辑文本并在 HTML 模板上查看时,它会显示代码视图而不是仅显示文本。 I'm new to Django so I'm not sure what I'm missing here.我是 Django 的新手,所以我不确定我在这里缺少什么。

My admin page where summernote is applied:我应用了summernote的管理页面:

管理页面

My html page where the above would be viewed:我的 html 页面将在其中查看上述内容:

html页面

admin.py:管理员.py:

from django.contrib import admin
from django_summernote.admin import SummernoteModelAdmin

from .models import Post

class PostAdmin(SummernoteModelAdmin):
    summernote_fields = ('body',)

admin.site.register(Post, PostAdmin)

models.py:模型.py:

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    ...

    body = models.TextField()

    ...

    def __str__(self):
        return self.title

EDIT: Where I display the "body" in my HTML template:编辑:我在 HTML 模板中显示“正文”的位置:

{% block body %}   

    ...

    <p>{{ post.body }}</p>

    ...

{% endblock %}

You will need to use safe filter here.您需要在此处使用安全过滤器。

Safe Marks a string as not requiring further HTML escaping prior to output. Safe在 output 之前将字符串标记为不需要进一步的 HTML escaping。 When autoescaping is off, this filter has no effect.当自动转义关闭时,此过滤器无效。

Your edit:您的编辑:

<p>{{ post.body | safe }}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM