gpt4 book ai didi

python - 如何防止 Django 基本内联自动转义

转载 作者:行者123 更新时间:2023-11-28 17:50:56 26 4
gpt4 key购买 nike

Django Basic Inlines 应用程序根据应用程序/模型/id 组合从伪 HTML 语法呈现预先确定的模板。例如,如果您正在撰写博客文章,则可以插入保存在图像模型中的图像:

# In the admin
This is the body of my post.

<inline type="media.image" id="1" class="full">

模板然后采用render_inlines 过滤器,它需要标记为safe 以便正确呈现HTML:

# Template
{{ post.body|render_inlines|safe }}

但即使使用 safe,过滤器仍然会转义 HTML,创建 <p><img src="..."><p>在源代码中。

根据文档,过滤器应该使用 mark_safe 来防止在过滤器级别自动转义,但是 parser.py 中的 inlines 函数已经使用 mark_safe

在 Django 1.4 中是否还需要一些东西来停止自定义过滤器层的自动转义?我似乎无法摆脱这种自动转义,无论是在

我尝试使用 autoescape=None,这似乎也没有帮助。

最佳答案

另一种解决方案是将新代码转换为 BeautifulSoup 对象,并替换为该对象。这样美丽的汤似乎表现正确。

这给你转义的 html:

soup = BeautifulSoup(html_doc)
body = soup.body
new_html = """<p> this is some deap code</p><a href="#">Pointless even</a>"""
body.replaceWith(new_html)

这为您提供了未转义的 html:

soup = BeautifulSoup(html_doc)
body = soup.body
new_html = """<p> this is some deap code</p><a href="#">Pointless even</a>"""
body.replaceWith(BeautifulSoup(new_html))

关于python - 如何防止 Django 基本内联自动转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9939248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com