:-) 偶系打杂滴,这两天研究下 django,哈哈~
通讯模型浏览器中我们输入:http://192.168.24.197:8888/query?key=value
其实是给服务器发送了 HTTP 请求。django 的处理模型如下:
ie/firefox ----> django ---- HttpRequest ----> user app
<---- django <--- HttpResponse --------/
----------------------------------------------
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django");
----------------------------------------------
结合上面一段小 app 来看,其中 request 就是 django.http.HttpRequest。浏览器请求的数据,会被丢到 HttpRequest 中,发送给 user app 处理,处理后返回一个 HttpResponse 即可。
HttpRequest 简介HttpRequest.path - /query/
.method - "GET"
.GET - {"key":"value"}
.COOKIES - ...
HttpResponse 简介简单的页面内容:
HttpResponse("hello django")
HttpResponse("hello django", mimetype="text/plain")
连续的页面内容:
response = HttpResponse()
response.write("<p>Here's the text of the Web page.</p>")
response.write("<p>Here's another paragraph.</p>")
修改 HTTP header (非 html 里面的<head>):
response = HttpResponse()
response['X-DJANGO'] = "It's the best."
参考http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/tut01.htmlhttp://docs.djangoproject.com/en/dev/ref/request-response/
评论