1.需求背景:
项目需要提供web service 接口,但是只需要获取信息,返回格式为xml,安全性要求也很低,所以没必要使用django rest api库(piston,tastypie等),决定写个简单的,直接用django 的mtv试试。
2.出现问题:
虽然模板是写的xml格式,但是访问的时候,并不是xml的,而是一堆字符串,并没有xml标签...
希望为如下形式:
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Copyright w3school.com.cn --> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
但实际显示成如下:George John Reminder Don't forget the meeting!
3.问题原因:
原因很简单,是django render的时候,不认为他是一个xml文件,所以就变成这样
4.解决方法:
render_to_response中加入mimetype参数,告知django返回数据类型return render_to_response('user_role/api_user.xml',mimetype="application/xml")