location: django.http
1. HttpResponse(content= ' ', mimetype=DEFAULT_CONTENT_TYPE)
例子:
res = {" success": False, "error": " "}
return HttpRepsponse(json.dumps(res), mimetype="application/json")
response = HttpResponse("Here's the text of the Web page.")
response = HttpResponse("Text only, please.", content_type="text/plain")
e = "error http request"
return HttpResponse("Error: %s" %e)
response = HttpResponse()
response.write("<p>error message.</p>")
在Django1.7中:
return HttpRpesponse(json.dumps(json), content_type="application/json")
2.response返回一个file附件
下面的例子返回一个excel文件
response = HttpResponse(mydata, content_type="application/vnd.ms-excel")
response['Content-Disposition'] = 'attachment; filename="foo.xls" '
3.FileResponse
from django.http import FileResponse
response = FileResponse(open('myfile.png', 'rb'))