`
54yuri
  • 浏览: 28228 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【转帖+改造】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.WSGIHandler

http 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 HttpResponse
def 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/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics