免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2370 | 回复: 2
打印 上一主题 下一主题

使用模板动态生成html文件无法保存和字符转义问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-16 09:23 |只看该作者 |倒序浏览
    htmlresponse = render_to_response('htmltemplate.html',{'ADLchs': ADLaunchs})
    htmlresponse ['Content-Disposition'] = 'attachment; filename="my.html"'
    htmlresponse .write(htmlresponse .content)
存在如下几个问题:
1、直接使用Httpresponse对象的write, filename没有生成!
2、使用filehandle = Open('filepth')
     通过文件filehandle.write(htmlresponse.content) ;生成的文件特殊字符被转义

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2012-10-16 11:11 |只看该作者
HttpResponse.write(content)
This method makes an HttpResponse instance a file-like object.


这不是生成文件, 是让HttpResponse像是一个文件句柄一样, 你可以向它write一些内容, 之后return返回这个HttpReponse对象, 那么你write的内容就被发送向浏览器了, 比如这样:
  1. from django.http import HttpResponse

  2. def index(request):
  3.     response = HttpResponse()
  4.     response.write('hello world')
  5.     return response
复制代码
页面上显示hello world, 和直接return HttpResponse('hello world')是一样的.

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
3 [报告]
发表于 2012-10-16 11:21 |只看该作者
哪里转义了? 具体说一下?

除了你给模板传入的变量会被转义以外, 其他东西是不会自动转义的.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP