Forums

Django CMS - WSGI configuration problem

Hello. I'm trying to get a Django CMS installation to serve pages from my URL http://newamericanmodelbond.pythonanywhere.com

I've read up on what WSGI is and what it's used for but I'm confident that the contents of the WSGI file aren't correct. Any help is greatly appreciated.

Here is the contents of wsgi.py

import os
import sys

path = '/home/NewAmericanModelBond/tutorial-project/mysite'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from application import app as application
application = application()

Here is the contents of my site's error.log

2021-05-16 20:13:07,568: Error running WSGI application
2021-05-16 20:13:07,589: ModuleNotFoundError: No module named 'application'
2021-05-16 20:13:07,589:   File "/var/www/newamericanmodelbond_pythonanywhere_com_wsgi.py", line 18, in <module>
2021-05-16 20:13:07,590:     from application import app as application

[formatted by admin]

Is there an file called application.py in /home/NewAmericanModelBond/tutorial-project/mysite? If not, that is the source of your problem. You need to import your application variable from a location that exists.

No, application.py doesn't exist in that directory. wsgi.py exists in that folder which includes the comment: It exposes the WSGI callable as a module-level variable named application. and the following code:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_wsgi_application()

Is that what I need? If so, what is the right import statement that should be added to /var/www/newamericanmodelbond_pythonanywhere_com_wsgi.py

Replacing last two lines from the wsgi.py quoted in the first post by

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

should be enough.

Thank you. I made the update that you suggested but there is still a problem. Here is the current contents of wsgi.py

import os
import sys

path = '/home/NewAmericanModelBond/tutorial-project/mysite'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Can you tell me where the system is looking for mysite.settings? Here is the contents of my site's error.log

2021-05-17 17:04:54,745: Error running WSGI application
2021-05-17 17:04:54,752: ModuleNotFoundError: No module named 'mysite.settings'
2021-05-17 17:04:54,752:   File "/var/www/newamericanmodelbond_pythonanywhere_com_wsgi.py", line 16, in <module>
2021-05-17 17:04:54,752:     application = get_wsgi_application()
2021-05-17 17:04:54,752: 
2021-05-17 17:04:54,752:   File "/home/NewAmericanModelBond/env/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2021-05-17 17:04:54,752:     django.setup(set_prefix=False)
2021-05-17 17:04:54,752: 
2021-05-17 17:04:54,752:   File "/home/NewAmericanModelBond/env/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
2021-05-17 17:04:54,752:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2021-05-17 17:04:54,753: 
2021-05-17 17:04:54,753:   File "/home/NewAmericanModelBond/env/lib/python3.6/site-packages/django/conf/__init__.py", line 82, in __getattr__
2021-05-17 17:04:54,753:     self._setup(name)
2021-05-17 17:04:54,753: 
2021-05-17 17:04:54,753:   File "/home/NewAmericanModelBond/env/lib/python3.6/site-packages/django/conf/__init__.py", line 69, in _setup
2021-05-17 17:04:54,753:     self._wrapped = Settings(settings_module)
2021-05-17 17:04:54,753: 
2021-05-17 17:04:54,754:   File "/home/NewAmericanModelBond/env/lib/python3.6/site-packages/django/conf/__init__.py", line 170, in __init__
2021-05-17 17:04:54,754:     mod = importlib.import_module(self.SETTINGS_MODULE)

Make sure that you have a settings.py file in /home/NewAmericanModelBond/tutorial-project/mysite/mysite

There was no folder /home/NewAmericanModelBond/tutorial-project/mysite/mysite so I created it and coped settings.py from /home/NewAmericanModelBond/tutorial-project/mysite to /home/NewAmericanModelBond/tutorial-project/mysite/mysite. That caused a new error:

2021-05-17 17:57:46,044: Error running WSGI application
2021-05-17 17:57:46,046: django.core.exceptions.ImproperlyConfigured: The app module <module 'mysite' (namespace)> has multiple filesystem locations (['./mysite', '/home/NewAmericanModelBond/tutorial-project/mysite/mysite', '/home/NewAmericanModelBond/mysite']); you must configure this app with an AppConfig subclass with a 'path' class attribute.

So I deleted the ./mysite/mysite folder and modified the path variable in wsgi.py and trimmed off "/mysite" from the end of it. This is what it looks like now:

path = '/home/NewAmericanModelBond/tutorial-project'

That seems to have resolved the WSGI issue because now there is a DB table error being reported from the CMS that appears to be much like the problem discussed on stackoverflow here so I'll pursue that as a separate issue. Thanks for all your help!

Yes, that sounds like the right solution. It doesn't really matter which directory your project is in (and which subdirectory its settings file is in), so long as the path specification in the WSGI file matches what you have, and what you ended up with sounds exactly how it should be.