WSGI in Django

Osirus Djodji
2 min readMay 28, 2022

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.The Python community came up with WSGI as a standard interface that modules and containers could implement. WSGI is now the accepted approach for running Python web applications.

WSGI gives you flexibility. Application developers can replace web stack components with others. This separates the choice of framework from the choice of web server, freeing users to choose a pairing that suits them, while freeing framework developers

WSGI servers promote scaling. WSGI servers handle processing requests from the web server and decide how to communicate those requests to an application framework’s process. Separation of responsibilities is important for effective scaling of web traffic.

In Django

​​When the WSGI server loads your application, Django must import the settings module — this is where your entire application is defined.

Django uses the DJANGO_SETTINGS_MODULE environment variable to locate the appropriate settings module. It should contain the dotted path to the settings module. According to your preference or according to the needs of your architecture you can use a different value for development and production.

WSGI servers get the callable application path from their configuration. Django’s built-in server, namely the runserver command, reads it from the WSGI_APPLICATION parameter.

The following sample show an excerpt of a wsgi.py file in a Django project.

import osfrom django.core.wsgi import get_wsgi_applicationos.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘proj_dir.settings’)application = get_wsgi_application()

proj_dir is the project directory created when running the startproject command. This is also the folder where your wsgi file is located.

The wsgi file is automatically generated by Django when creating your project. You need to set things up correctly based on your environment to run your app properly.

--

--