我需要使用render_to_response返回的HTML才能逃脱。我找不到任何合适的文档。有人可以指向某个方向吗?

代码是:

return render_to_response(template_name, {return render_to_response(template_name, {
            'form': form,
            redirect_field_name: redirect_to,
            'site': current_site,
            'site_name': current_site.name,
        }, context_instance=RequestContext(request))

在这里,我需要响应html文本才能逃脱。我知道的是在字符串中读取模板文件,然后用re.escape()将其逃脱,然后呈现。什么是一种更清洁,更简单的方法?

有帮助吗?

解决方案

字符串通过视图传递到模板 render_to_response 默认情况下被逃脱。

看: http://docs.djangoproject.com/en/dev/topics/templates/#id2

默认情况下,在Django中,每个模板都会自动逃脱每个变量标签的输出。具体来说,这五个字符被逃脱了:

< is converted to &lt;
> is converted to &gt;
' (single quote) is converted to &#39;
" (double quote) is converted to &quot;
& is converted to &amp;

同样,我们强调这种行为默认情况下。

其他提示

听起来您希望逃脱全部响应 - 是真的吗?似乎有些奇怪,但是您当然可以做到。

import django.utils.html as html
string = render_to_string(<all of the args to render_to_response>)
escaped_string = html.escape(string)
return HttpResponse(escaped_string)

我不太确定另一端的人/浏览器会 有了这个,但这听起来像是您要的。

您可以使用 escape 模板过滤器或 autoescape 模板标签。看 http://docs.djangoproject.com/en/dev/ref/templates/builtins/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top