What is the use of wsgi.py file that is there in django application when created using django-admin startproject
Asked Answered
P

3

5

Can anyone help me in understanding what is the use of wsgi.py file?

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.local")

application = get_wsgi_application()
Phalange answered 19/4, 2020 at 19:28 Comment(0)
L
7

This file is basically used for deployment only as is not used in development, except when you are using docker.

It is used as an interface between application server to connect with django or any python framework which implements wsgi specified by python community, taking about usage mostly you will see it is used with gunicorn

Lithotrity answered 19/4, 2020 at 19:31 Comment(2)
Sorry, but wsgi file is actually used in default development environment of Django. Source->docs.djangoproject.com/en/dev/ref/django-admin/…Adkisson
Vishwas - the docs say the WSGI object specified by settings.WSGI_APPLICATION is used - it doesn't say the wsgi.py file specifically is used. Couldn't you ditch the file and move the construction of the WSGI app object somewhere else, then update the setting to point to that new location?Kitchens
W
2

The main use of deploying with WSGI is the application callable which the application server uses to communicate with your code. It’s commonly provided as an object named application in a Python module accessible to the server.

Warhol answered 19/4, 2020 at 20:41 Comment(0)
K
0

The WSGI.py and ASGI.py are there to define the application entry point according the the Python specs depending on whether your are running a synchronous server based on WSGI or asynchronous server based on ASGI.

In my experience, if you move/rename the files, even in development, without updating the settings.py file with the new location/name you will receive an error indicating the file cannot be found.

For example, here is the error message when attempting to run the local built in development server after I removed the WSGI.py file, proving that the files are required for development.

Here is the pertinent part of the error message for an project named 'main':

get_internal_wsgi_application raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: WSGI application 'main.wsgi.application' could not be loaded; Error importing module.

Kimball answered 30/1 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.