视图
最基本的视图(View),接受一个 HttpRequest 对象,返回一个 HttpResponse 对象:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
HttpRequest
伪 Python 代码示意:
class HttpRequest:
self.scheme # 'http', 'https'
self.body # 原始 body,是 bytestring
self.path # 请求路径,不含 scheme、domain 或 query string,如 "/music/bands/the_beatles/"
self.path_info
self.method # 'GET', 'POST'
self.encoding
self.content_type # MIME 类型
self.content_params
self.GET # QueryDict 对象,是 URL 中 ? 后的部分
self.POST # 同上
self.COOKIES
self.FILES
self.META
self.headers
self.resolver_match