<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: A H</title>
    <description>The latest articles on DEV Community by A H (@aschert).</description>
    <link>https://dev.to/aschert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F768938%2F14c475f1-341f-4e7e-b496-59016a39dfe1.jpeg</url>
      <title>DEV Community: A H</title>
      <link>https://dev.to/aschert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aschert"/>
    <language>en</language>
    <item>
      <title>Some insights into the low-level workflow of the Django framework</title>
      <dc:creator>A H</dc:creator>
      <pubDate>Fri, 10 Dec 2021 03:42:55 +0000</pubDate>
      <link>https://dev.to/aschert/some-insights-into-the-low-level-workflow-of-the-django-framework-3p85</link>
      <guid>https://dev.to/aschert/some-insights-into-the-low-level-workflow-of-the-django-framework-3p85</guid>
      <description>&lt;h2&gt;
  
  
  Running flow
&lt;/h2&gt;

&lt;p&gt;You can use multiple methods to run a Django app, one of which is &lt;code&gt;runserver&lt;/code&gt; which uses the web server of Django itself, and it is mostly used in the development and testing phases. Another is using protocols like &lt;code&gt;fastcgi&lt;/code&gt; and &lt;code&gt;uWSGI&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. runserver
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;: &lt;code&gt;python3 manage.py runserver&lt;/code&gt;, this command is run under port 8080 by default. If you investigate into the source codes of &lt;code&gt;manage.py&lt;/code&gt;, you will find that this line is actually executed through the internal &lt;code&gt;runserver&lt;/code&gt; command in &lt;code&gt;execute_from_command_line&lt;/code&gt;, and this command mostly does two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parse the parameters, and get the &lt;code&gt;wsgi handler&lt;/code&gt; by &lt;code&gt;django.core.servers.basehttp.get_internal_wsgi_application&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Generate a &lt;code&gt;WSGIServer&lt;/code&gt; object according to your IP address and port, and then accept the user's requests. The source codes of &lt;code&gt;get_internal_wsgi_application&lt;/code&gt; is as followed:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_internal_wsgi_application&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.conf&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;
    &lt;span class="n"&gt;app_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;WSGI_APPLICATION&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;app_path&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;get_wsgi_application&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;import_by_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;app_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;error_prefix&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;WSGI application &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;%s&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; could not be loaded; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;app_path&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By the above we know that, &lt;code&gt;Django&lt;/code&gt; will first obtain the &lt;code&gt;handler&lt;/code&gt; via &lt;code&gt;WSGI_APPLICATION&lt;/code&gt; in &lt;code&gt;settings&lt;/code&gt;. While building the &lt;code&gt;project&lt;/code&gt;, &lt;code&gt;Django&lt;/code&gt; will create a &lt;code&gt;wsgi.py&lt;/code&gt; by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. uWSGI
&lt;/h3&gt;

&lt;p&gt;Another approach is the most prevalent one while running Django in production, it uses Ngnix combined with uWSGI. To dive more deeply into this method, we would need to learn more about two protocols: &lt;code&gt;WSGI&lt;/code&gt; and &lt;code&gt;uWSGI&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WSGI&lt;/strong&gt; is short for &lt;strong&gt;Web Server Gateway Interface&lt;/strong&gt;. It is a universally applicable interface that bridges python-based web servers and web apps/framworks, and is designed based on the current CGI standard. &lt;code&gt;WSGI&lt;/code&gt; is actually a gateway, which helps conversion between multiple protocols.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;uWSGI&lt;/strong&gt; is a web server which supports protocols like WSGI protocol, UWSGI protocol and HTTP Protocol, etc. It is known for rapid transmission, low memory usage and its excellence in multi-app management.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  HTTP Request Process
&lt;/h2&gt;

&lt;p&gt;Django's RESTful workflow is similar to other web frameworks: Receiving requests and returning responses. Let's see the details in the following:&lt;/p&gt;

&lt;p&gt;When Django is creating the project via &lt;code&gt;django-admin&lt;/code&gt;, it will automatically create some default files including &lt;code&gt;settings.py&lt;/code&gt; and &lt;code&gt;manage.py&lt;/code&gt;, and execute the following command before building the WSGIServer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.conf&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During the execution of the above command, Django will read the &lt;code&gt;DjANGO_SETTINGS_MODULE&lt;/code&gt; config info in &lt;code&gt;os.environ&lt;/code&gt; and load the config files before creating the &lt;code&gt;setting&lt;/code&gt; object. Therefore, inside the &lt;code&gt;manage.py&lt;/code&gt;, it will first add the path of project settings to the os path before obtaining the WSGIServer.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Creating the WSGIServer
&lt;/h3&gt;

&lt;p&gt;No matter you are running your Django project with runserver or uWSGI, Django will always call &lt;code&gt;run()&lt;/code&gt; in &lt;code&gt;django.core.servers.basehttp&lt;/code&gt; during the start-up, creating an instance of &lt;code&gt;WSGIServer&lt;/code&gt;, and later call its &lt;code&gt;serve_forever()&lt;/code&gt; to start the HTTP services. Below is the source codes of &lt;code&gt;run()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wsgi_handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ipv6&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="n"&gt;server_address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;httpd_cls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;WSGIServer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socketserver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ThreadingMixIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WSGIServer&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;httpd_cls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WSGIServer&lt;/span&gt;
        &lt;span class="n"&gt;httpd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;httpd_cls&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WSGIRequestHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ipv6&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ipv6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# Sets the callable application as the WSGI application that will receive requests
&lt;/span&gt;        &lt;span class="n"&gt;httpd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wsgi_handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;httpd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serve_forever&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above, we see that: While creating the &lt;code&gt;WSGIServer&lt;/code&gt; instance, &lt;code&gt;WSGIRequestHandler&lt;/code&gt; is used to designate the HTTP request handler. When the user's request arrives at the server, &lt;code&gt;WSGIServer&lt;/code&gt;will create a &lt;code&gt;WSGIRequestHandler&lt;/code&gt; instance, using its &lt;code&gt;handler&lt;/code&gt; function to process the http request, and actually in the end it calls &lt;code&gt;run()&lt;/code&gt; in &lt;code&gt;wsgiref.handlers.BaseHandler&lt;/code&gt;. With &lt;code&gt;set_app()&lt;/code&gt;, &lt;code&gt;WSGIServer&lt;/code&gt; set up a callable object as an application. The above mentioned &lt;code&gt;handler&lt;/code&gt; function will call this application to process the request and return the response. Besides, &lt;code&gt;WSGIServer&lt;/code&gt; inherits from &lt;code&gt;wsgiref.simple_server.WSGIServer&lt;/code&gt;, but &lt;code&gt;WSGIRequestHandler&lt;/code&gt; inherits from &lt;code&gt;wsgiref.simple_server.WSGIRequestHandler&lt;/code&gt;, &lt;code&gt;wsgiref&lt;/code&gt; is the reference implementation of &lt;code&gt;WSGI&lt;/code&gt; from the Python standard library.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Process the request
&lt;/h3&gt;

&lt;p&gt;In Django, the aforementioned &lt;code&gt;application&lt;/code&gt; is in general an object of &lt;code&gt;wsgi.WSGIHandler&lt;/code&gt; in &lt;code&gt;django.core.handlers&lt;/code&gt;. &lt;code&gt;WSGIHandler&lt;/code&gt; inherits from &lt;code&gt;base.BaseHandler&lt;/code&gt; and it creates a &lt;code&gt;WSGIRequest&lt;/code&gt; instance which further inherits from &lt;code&gt;http.HttpRequest&lt;/code&gt;. These are the essenial logics on how Django processes the request.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Return the response
&lt;/h3&gt;

&lt;p&gt;There is a &lt;code&gt;get_response&lt;/code&gt; method in &lt;code&gt;BaseHandler&lt;/code&gt; and it initially loads the &lt;code&gt;ROOT_URLCONF&lt;/code&gt; in the Django project and then find out corresponding &lt;code&gt;view&lt;/code&gt; methods or classes according to the url rules. The logics in the &lt;code&gt;view&lt;/code&gt; functions/classes will create and return specific responses through the request instance.&lt;/p&gt;

&lt;p&gt;After Django returns the results, the &lt;code&gt;run()&lt;/code&gt; in &lt;code&gt;wsgiref.handlers.BaseHandler&lt;/code&gt; will call &lt;code&gt;finish_response&lt;/code&gt; to end the request, and return the contents to the user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m528j1i3u1dexslgs4h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m528j1i3u1dexslgs4h.png" alt="Image description" width="467" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1xycdlvuq6rlhr5xop4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1xycdlvuq6rlhr5xop4.png" alt="Image description" width="531" height="699"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seeing from the above image. 1-9 represent 9 steps, and the first steo is to send the request from a page, which reaches the &lt;code&gt;Request&lt;/code&gt; middleware in the 2nd step. In the 3rd step, &lt;code&gt;URLConf&lt;/code&gt; is querying for the corresponding &lt;code&gt;View&lt;/code&gt; in &lt;code&gt;url.py&lt;/code&gt;, and later the &lt;code&gt;View&lt;/code&gt;s are called to answer the request with their functions invoked. During the 6th step, the &lt;code&gt;view&lt;/code&gt; will access the model information and any interactions between model and databases are done by a built-in manager. Here &lt;code&gt;View&lt;/code&gt;s can have contexts if needed, and if there is any context, it will be passed to &lt;code&gt;Template&lt;/code&gt; for further rendering.&lt;/p&gt;

&lt;p&gt;Processes a-e are done mostly in &lt;code&gt;Template&lt;/code&gt;s. First filters and tags are used to render the output, which is later returned to &lt;code&gt;View&lt;/code&gt;s. At the same time, &lt;code&gt;Response&lt;/code&gt; will receive instances of &lt;code&gt;HTTPResponse&lt;/code&gt;, and any response could add up new information or simply return a brand new body. In the end, user could receive the response from the browser.&lt;/p&gt;

&lt;p&gt;Two most important components in these procedures are: Middleware and URLConf, and below we will briefly walk through them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Middleware
&lt;/h1&gt;

&lt;p&gt;Middleware does not exclusively belong to Django, but also exist in other frameworks. In Django, middleware could participate in 4 phases in the RESTful process: request, view, response, exception. Accordingly, every class of middleware has the following 4 methods in the pattern of: &lt;code&gt;process_x&lt;/code&gt;, where x are &lt;code&gt;request&lt;/code&gt;, &lt;code&gt;view&lt;/code&gt;, &lt;code&gt;response&lt;/code&gt; and &lt;code&gt;exception&lt;/code&gt; You can define one or more of these methods depending on which stage you want the middleware to put effect on, and every method returns a response object.&lt;/p&gt;

&lt;h1&gt;
  
  
  URLConf
&lt;/h1&gt;

&lt;p&gt;URLconf is the mapping of the websites supported by Django. Basically, it is a mapping table between the URL pattern and the &lt;code&gt;view&lt;/code&gt; functions.&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;[1] &lt;a href="https://hitesh.in/2009/django-flow/" rel="noopener noreferrer"&gt;https://hitesh.in/2009/django-flow/&lt;/a&gt;&lt;br&gt;
[2] &lt;a href="https://stackoverflow.com/questions/1836627/diagrams-explanations-of-django-request-processing" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/1836627/diagrams-explanations-of-django-request-processing&lt;/a&gt;&lt;br&gt;
[3] &lt;a href="https://docs.djangoproject.com/en/3.2/topics/http/urls/" rel="noopener noreferrer"&gt;https://docs.djangoproject.com/en/3.2/topics/http/urls/&lt;/a&gt;&lt;br&gt;
[4] &lt;a href="https://docs.djangoproject.com/en/3.2/topics/http/middleware/" rel="noopener noreferrer"&gt;https://docs.djangoproject.com/en/3.2/topics/http/middleware/&lt;/a&gt;&lt;br&gt;
[5] &lt;a href="https://docs.djangoproject.com/en/3.2/topics/templates/" rel="noopener noreferrer"&gt;https://docs.djangoproject.com/en/3.2/topics/templates/&lt;/a&gt;&lt;br&gt;
[6] &lt;a href="https://medium.com/@jvdali966/understanding-djangos-work-flow-1ee521422092" rel="noopener noreferrer"&gt;https://medium.com/@jvdali966/understanding-djangos-work-flow-1ee521422092&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
