python - 在 Django 中,如何在 HttpResponse 中转义 html?

标签 python django django-templates escaping httpresponse

我的一个 View 中的以下代码返回未转义的 html 字符串,由于它是 Ajax 请求,因此无法在前端进行解析。

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))

更正此问题的最简单方法是什么? 提前致谢..

最佳答案

Lakshman Prasad 的回答在技术上是正确的,但有点麻烦。一个更好的转义文本的方法是(正如上面miku的评论所建议的):

from django.utils.html import escape
return HttpResponse(escape(some_string))

关于python - 在 Django 中,如何在 HttpResponse 中转义 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1946281/

相关文章:

python - 返回字符串列表的 python 函数有问题吗?

python - 如何手动使用 make_password 和 check_password?

python - 使用模型实例中的数据加载 Django 表单对象 "loaded"

python - Django静态图像不显示

python - 在 Python 中实现 argmax

python - python 如果该类别的 value_count 小于 10,则用常量替换列值

python - 如何从字符串列表中保存随机字符串?

mysql - 如何暂时禁用 MySQL 中的外键约束?

javascript - 上传多个文件并显示进度条

django - Django 模板中的动态分页