How to Solve Error wsgi.py does not contain WSGI application ‘application’ on executing Django Application

Posted on

The error on executing Django application is actually specified on the title of this article. It is actually about how to solve error message related to ‘wsgi.py’ file which is responsible for describing or defining application which is going to be executed in Apache Webserver. Basically, in the general understanding, in order to deploy a python-based application specifically in this context it is a Django-based framework, the usage of file description or file definition on deploying the application itself in a Webserver such as Apache Webserver is a must. The error as shown in the title of the article can be presented below :

mod_wsgi (pid=20336): Target WSGI script '/wsgi.py' does not contain WSGI application 'application'.

So, the error is caused by a file named wsgi.py which doesn’t contain any WSGI application called ‘application’. Before solving the error further, In this context, a python-based application has a a key concept for using an application callable where it is actually an application server uses for communicating with the code. The application itself actually is an object named ‘application’ exist in a Python module available for further access to the server. Normally, using the ‘startproject’ command in Django framework, it will create not only the project folder itself but a wsgi.py located in it where the content of the file itself specifies an application callable. So, upon executing the following command :

django-admin startproject project-name

If, it is executed as the following example which is shown as follows :

user@hostname:~/tmp$ django-admin startproject testing
user@hostname:~/tmp$ cd testing/
user@hostname:~/tmp/testing$ ls
manage.py testing
user@hostname:~/tmp/testing$

In the above output generated, the content is presented the location of the wsgi.py file will exists in the following :

user@hostname:~/tmp/testing$ cd ..
user@hostname:~/tmp$ tree -L 3
.
└── testing
    ├── manage.py
    └── testing
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

2 directories, 5 files
user@hostname:~/tmp$

The file definition is representing how a Python-based application module is called or is deployed. Below is the content of the file :

"""
WSGI config for testing project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")

application = get_wsgi_application()

 

The application callable itself is pointed out in the last line. If the line doesn’t exist, it will actually generates error as shown in the title of the article. So, in order to solve the problem, just check the wsgi.py file and add the line above. It is actually worked and tested for application powered by Django framework.

One thought on “How to Solve Error wsgi.py does not contain WSGI application ‘application’ on executing Django Application

Leave a Reply