【转帖+改造】django HTTP Request 的调用层次
from http://kasicass.blog.163.com/blog/static/3956192009510115226144/
django.core.management.base.py, commands 的基类,定义了几个基础接口
django.core.management.[commands], 里面实现了 startproject, runserver 等所有的 commands
django.core.management.__init__.py 中的 execute_manager() 是所有逻辑的入口。
python manager.py runserver ip:port ==> django.core.management.__init__.py django.core.management.commands.runserver.py django.core.servers.basehttp.run() httpd = WSGIServer(server_address, WSGIRequestHandler) httpd.set_app(wsgi_handler) # wsgi_handler = AdminMediaHandler(WSGIHandler(), path) httpd.serve_forever() # django.core.handlers.wsgi.WSGIHandlerhttp request => django.core.servers.basehttp.WSGIRequestHandler.handle() django.core.servers.basehttp.ServerHandler.run() django.core.servers.basehttp.AdminMediaHandler.__call__() django.core.handlers.wsgi.WSGIHandler.__call__() request = self.request_class(environ) 【1】 response = self.get_response(request) 【2】 django.core.handlers.base.BaseHandler.get_response() resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) callback, callback_args, callback_kwargs = resolver.resolve(request.path_info) response = callback(request, *callback_args, **callback_kwargs) django.core.servers.basehttp.ServerHandler.run(), response 会被一路 return,到达这里后,拼接,再发回给浏览器。
urlpatterns = patterns('', (r'^hello/$', 'foobar.helloworld.index'),)
from django.http import HttpResponsedef index(request): return HttpResponse("Hello Django!")
def login(request): m = Member.objects.get(username=request.POST['username']) if m.password == request.POST['password']: request.session['member_id'] = m.id return HttpResponse("You're logged in.") else: return HttpResponse("Your username and password didn't match.")