Django source code analysis-guide

Source: Internet
Author: User
Disclaimer: This article provides two running methods for reprint Django.
1. Run the built-in web server through Python manage. py runserver
2. Use mod_python

Both methods, but the last request generation method is inherited from Django. http. httpreques
First, the runserver parameter is actually executed by the Django. Core. Management. commands. runserver module.
Key in runserver. pyCodeIs
Run (ADDR, INT (port), Handler)

Run Function Definition (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 ()
It is not difficult to find that an httpserver object is generated and wsgi_handler is used as the HTTP request handle.

The second method is to take Apache as an example, which is more concise.

<Location "/">
Sethandler Python-Program
Pythonhandler Django. Core. Handlers. modpython
Setenv django_settings_module mysite. Settings
Pythondebug on
</Location>
Pythonhandler specifies Django. Core. Handlers. modpython to handle the handle of each HTTP Request
Django. Core. Handlers. modpython defines modpythonhandler and modpythonrequest

Modpythonhandler and wsgihandler have the same content.
First, generate the request object, and then process various middleware and return response. The key code is as follows:
Try:
Try:
Request = self. request_class (Environ)
Except t 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 is defined in the Django. Core. Handlers. Base. py basehandler class:
Def GET_RESPONSE (self, request)
The key code of this function is as follows:
Resolver = urlresolvers. regexurlresolver (R' ^/', urlconf)
Try:
Callback, callback_args, callback_kwargs = resolver. Resolve (
Request. path_info) # Here is the view function, that is, the specific processing function for each URL defined in URLs. py.

# Apply view middleware # process 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) # execute the view Function
Except t exception, E:
# If the view raised an exception, run it through exception
# Middleware, and if the exception middleware returns
# Response, use that. Otherwise, reraise the exception.
For middleware_method in self. _ prediction_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
T 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 # Return response

The figure is clear and helpful for understanding the Django processing process.

 



Write it here first, and analyzeSource codeIt is indeed helpful to understand Django by yourself and find that the Django source code is easy to understand. I think this has a lot to do with the Python language itself.

 
 
 
 
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.