Why do we have to provide WSGI_APPLICATION variable in Django settings

Wendy picture Wendy · Mar 19, 2016 · Viewed 7.7k times · Source

I'm a beginner Django developer so if this question doesn't make sense please forgive me.

We provide a variable called WSGI_APPLICATION in django settings along with ROOT_URLCONF and some other settings variables. and we provide settings file path in wsgi.py file as well,

import os
import django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGSS_MODULE", "<settings_file_path>")
application = get_wsgi_application()

So you see, its a two way connection.right?

I mean path of settings file in wsgi file and path of wsgi file in settings file. so why do we have to do this.

as per my understanding path of settings file in wsgi file should be good enough and we dont need that extra variable in django settings?

ultimately the wsgi file is the starting point of django application, right?

PLease correct me wherever I'm wrong.

Answer

Kevin Christopher Henry picture Kevin Christopher Henry · Mar 19, 2016

Ultimately the wsgi file is the starting point of django application, right?

Not necessarily. The documentation for WSGI_APPLICATION explains what this setting is for:

The full Python path of the WSGI application object that Django’s built-in servers (e.g. runserver) will use.

Any server you set up has to know where the WSGI file is. If you're using an external server it will look in its own settings. If you're using Django's development server, it will check Django's settings. So the circularity you noticed is a consequence of the fact that the Django application can be started in a number of different ways.