首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

【转帖+改建】django HTTP Request 的调用层次

2013-01-23 
【转帖+改造】django HTTP Request 的调用层次from http://kasicass.blog.163.com/blog/static/395619200951

【转帖+改造】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,到达这里后,拼接,再发回给浏览器。


我们的小程序 helloworld.index() 就是上面的 callback 函数。
-------------------------------------------------
# urls.py
urlpatterns = patterns('',    (r'^hello/$', 'foobar.helloworld.index'),)


-------------------------------------------------
# helloworld.py
from django.http import HttpResponsedef index(request):    return HttpResponse("Hello Django!")


-------------------------------------------------

WSGI 是啥,看:
http://cvs.woodpecker.org.cn/svn/woodpecker/pureprolog/trunk/essay/wsgiinto/wsgi_into.html
http://wiki.python.org/moin/WSGIImplementations


==================================================================================
//我的注释:奇怪吗,在上面生成response的过程当中,又再次生成了request,看上面的【1】,原因在于:我们对request对象当中进行的一些参数设置,要反馈到response当中去,比如cookie和session,这也不难理解为什么下面这样的代码,把我们设置的session 给回传给客户端了

 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.") 



via http://docs.djangoproject.com/en/dev/topics/http/sessions/

热点排行