Django源碼分析–引導

來源:互聯網
上載者:User
聲明:本文為轉載 django有兩種運行方式
1,通過python manage.py runserver運行內建的web server
2,通過mod_python

兩種方式但最後的request產生方法都是繼承自django.http.HttpReques
第一種方式, runserver參數最後實際執行的是django.core.management.commands.runserver模組
runserver.py中關鍵代碼為
run(addr, int(port), handler)

run函數詳細定義(django.core.servers.basehttp.py):
def run(addr, port, wsgi_handler):
server_address = (addr, port)
httpd = WSGIServer(server_address, WSGIRequestHandler)
httpd.set_app(wsgi_handler)
httpd.serve_forever()
不難發現,產生一httpServer對象,並將wsgi_handler作為http請求handle

第二種方式,以Apache為例那更簡明了

<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
</Location>
PythonHandler指明django.core.handlers.modpython為處理每個http請求的handle
django.core.handlers.modpython定義了ModPythonHandler和ModPythonRequest

ModPythonHandler和WSGIHandler內容基本相同
首先產生request對象,然後處理各中介軟體和返回response,關鍵代碼如下:
try:
try:
request = self.request_class(environ)
except UnicodeDecodeError:
response = http.HttpResponseBadRequest()
else:
response = self.get_response(request)

# Apply response middleware
for middleware_method in self._response_middleware:
response = middleware_method(request, response)
response = self.apply_response_fixes(request, response)
finally:
signals.request_finished.send(sender=self.__class__)

get_response定義在django.core.handlers.base.py BaseHandler類中:
def get_response(self, request)
該函數關鍵代碼如下:
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
try:
callback, callback_args, callback_kwargs = resolver.resolve(
request.path_info) #此處為尋找view函數,即urls.py中定義的每個url具體的處理函數

# Apply view middleware #處理中介軟體
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
return response

try:
response = callback(request, *callback_args, **callback_kwargs) #執行view函數
except Exception, e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
# response, use that. Otherwise, reraise the exception.
for middleware_method in self._exception_middleware:
response = middleware_method(request, e)
if response:
return response
raise
if response is None:
try:
view_name = callback.func_name # If it's a function
except AttributeError:
view_name = callback.__class__.__name__ + '.__call__' # If it's a class
raise ValueError, "The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name)

return response #返回response

有一張圖很明了,對理解django處理流程很有協助




先寫到這,分析原始碼對自己理解django確實協助大,發現django源碼很易懂,我想這也跟python語言本身有很大的關係

 
 
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.