Django Response範例
server返回的数值被封装在response中
HttpResponse应答对象主要属性:
content:返回给客户端html内容,默认是字符串。###html
charset:指定返回给客户端内容的字符集,默认是UTF-8。
status_code:响应HTTP状态代码。
reason_phrase:响应HTTP状态原因。
构造方法
HttpResponse(content=’’, content_type=None, status=200, reason=None, charset=None)
content_type是设置response的MIME类型,用于填充HTTP Content-Type标头。默认是text/html
是html調用browser打開
是xml用excel打開
沒有安裝就會下載檔案(download)
每一種文件都有對應的MIME類型
默认是charset=utf-8
範例
說明 : def hello return 一個response
from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
response = HttpResponse(“<h3>Hello, world. </h3>”, charset=’gbk’)###
response.charset = ‘gbk’ #’utf-8'字集廣泛
###response.charset與上一行charset=’gbk’只能擇一
response.write(‘<h3>世界你好。</h3>’)###
return response
response本身就是server端對client端的回應,回應html很基本的功能
HttpResponse(“<h3>Hello, world. </h3>”
response.write(‘<h3>世界你好。</h3>’)