learned today. Run a simple Django project with a simple Django framework and a simple WSGI server;
Django is not a complete Web backend framework, it needs to be matched with a WSGI server, which is the network communication part of the WSGI server.
1.
Django and Wsgi
WSGI Full Name: Web server Gateway Interface, an interface between Python-defined WSGI programs and WSGI servers
now a For WSGI applications, only 3 requirements can be met:
- is callable, such as a function, or an instance of a callable class (with a __call__ method)
- The WSGI application should return a value that can be iterated (iterable), such as a list of strings
- WSGI application should call the WSGI server incoming Start_response function to send the status code and HTTP headers before returning
one of the simplest to meet The WSGI protocol application needs to implement a function of the specified form:
fromWsgiref.simple_serverImportMake_serverdefWsgi_app (environ,start_response): fromPprintImportpprint pprint (environ) start_response ('OK',[('Context-type','Text/plain')]) return 'such a tiny Wsgi app!'httpd= Make_server ('0.0.0.0', 80, Wsgi_app)Print 'Start Server'Httpd.serve_forever ()
environ is a containing all A Dictionary of HTTP request information/dict, generated by an WSGI server unpacking an HTTP request.
2. Create The Wsgi Application Object :
The Django framework decomposes the structure of a WSGI application, some of which are done by the framework, and some that need to be implemented by the developer. Therefore, developing a Web application based on the Django framework is in fact the part that the Django framework has agreed to need to be done by the developer.
According to the Django Convention, there are two core components in a WSGI application: routing tables and views. The core function of the Django Framework is routing: Find the routing table based on the URL in the HTTP request, and distribute the HTTP request to different views to handle:
The Django framework relies heavily on a global configuration object settings to customize its behavior, so we need to initialize the global configuration object first using the default value before creating the Wsgi Application Object
from Import settings; # Configuration Objects from import get_wsgi_application; # Create a Wsgi app object = True# Config object = get_wsgi_application (); # Create a Wsgi app object Print Wsgi_app
3.
Writing View Functions
input : The first parameter is a HttpRequest object, which is the full encapsulation of the Django Framework for an HTTP request, and the view function extracts the information from the request from this object
output : The return value should be a HttpResponse object, and the Django framework will complete the response to the WSGI server based on the returned object
fromDjango.confImportSettings fromDjango.core.wsgiImportget_wsgi_application fromDjango.httpImporthttprequest,httpresponsesettings.configure () settings. DEBUG=Truewsgi_app=get_wsgi_application ()#a simple view functiondefV_index (Request):returnHttpResponse ('hello,djando!')#a HttpRequest object is constructed from a mock-up frame, passed to the view functionreq =HttpRequest ();PrintV_index (req)
4.
Defining the routing table
The Django framework finds the corresponding view function based on the URL of the HTTP request, and, naturally, the routing table uses a list object, each of which records a URL pattern corresponding to a view function ;
URL () function is used to generate a Route Item , the first argument is a regular expression that matches the The URL of the HTTP request, prefix r is used to prevent the regular string from being escaped, and the second parameter is The view function we defined.
To register a route:
in a somewhat scaled application, there may be multiple development groups, each of which maintains a separate routing table. Therefore, in the Django framework, you need to tell the Django framework to use that routing table as the root route table .
using the global configuration object's Root_urlconf property to register the root route table, you should specify a module name with a urlpatterns variable for this property, and Django will dynamically import the module and use its urlpatterns The value of the variable as the routing table.
therefore, in general, the routing table variable should always be named Urlpatterns.
ImportSYS fromDjango.confImportSettings fromDjango.core.wsgiImportget_wsgi_application fromDjango.httpImportHttprequest,httpresponse fromDjango.conf.urlsImporturlsettings.configure () settings. DEBUG=Truewsgi_app=get_wsgi_application ()defV_index (Request):returnHttpResponse ('hello,djando!') Urlpatterns=[url (r'^$', V_index),]settings. Root_urlconf= sys.modules[__name__]PrintUrlpatterns
5.
Docking
Simple
WSGI Server
:
Use a simple WSGI server that comes with Python:
from Import = make_server ('0.0.0.0',Wsgi_app) # Full address 0.0.0.0; Port number 80httpd.serve_forever ()
6.
Finally, create a
Django Implementation WSGI application
:
#-*-coding:utf-8-*-ImportSYS fromDjango.confImportSettings fromDjango.core.wsgiImportget_wsgi_application fromDjango.httpImportHttprequest,httpresponse fromDjango.conf.urlsImportURL fromWsgiref.simple_serverImportmake_serversettings.configure () settings. DEBUG=Truewsgi_app=get_wsgi_application ()defV_index (Request):returnHttpResponse ('hello,djando!') Urlpatterns=[url (r'^$', V_index), #r为防止正则字符串被转义,' ^$ ' in the Django framework before using the defined routing table, has eaten the/]settings of that prefix before the $ symbol. Root_urlconf= sys.modules[__name__]httpd= Make_server ('0.0.0.0', 80, Wsgi_app)Print 'Starting server ...'Httpd.serve_forever ()
Python Framework Django and WSGI