Deploying a Django project - WSGI problem

Hi there,

I’m having some hard time with deploying the website on python anywhere as part of ‘Build python web apps with Django course’. If visit the site it says “Something went wrong” and the error log looks like this:

What confuses me, is there is no module mysite and line 17 in WSGI is blank. I changed the mysite and yourname, i tried the debugg site, but its dead end (for me).

I used the djangovet files from github, and I think I followed the instructions in the course guide. Can anyone help please? :wink:

I’d double-check that, then, because it’s certainly looking for something with that name. Given it’s in the boilerplate repo provided, I wonder whether a save didn’t go through or something.

Hi, thanks for your reply. I went through the setting up again and discovered, that the site is functional until i paste the following code to settings.py from the codecademy course:

KEYSDIR = str(BASE_DIR)+"/keys.json" with open(KEYSDIR) as k: project_keys = json.loads(k.read()) def getKey(setting,project_keys=project_keys): try: return project_keys[setting] except KeyError: errorMessage = "Set the {} env var".format(setting) raise ImproperlyConfigured(errorMessage)

After that, the site crashes with the problems I mentioned earlier. I figured, that the site may run even without this part of code - its about hiding the secretcode in keys.json and this code should call that key from json.

Can you please provide the contents of your WSGI file? This is the file which you’re asked to create/update in step 15, below:

Hi so I think I solved this, it worked for me anyways.
In the settings.py file, leave the imports for json and ImproperlyConfigured at the top of the page, but move all the code that you shared starting from "KEYSDIR = str(BASE_DIR)+‘/keys.json’ and ending with "raise ImproperlyConfigured(errorMessage). Move all of that right below the code that looks like this:

Build paths inside the project like this: BASE_DIR / ‘subdir’.

BASE_DIR = Path(file).resolve().parent.parent

Quick-start development settings - unsuitable for production

See Deployment checklist | Django documentation | Django

put all the code here. And as you figured out, just make sure that the ‘yourname’ and the two ‘mysite’ parts got changed. and make sure you save the file, and click the ‘reload yourname.pythonanywhere.com’ button to make sure it’s saved.
We are having this problem because the lesson says to put the imports at the top of the page, but does not specify where to put the rest of the code, so I ended up putting the rest of the code above where the BASE_DIR gets defined.
after I did this, the web app worked for me. Hope this helps (:
here’s a screen shot.

I have a problem with WSGI!

I got the error:

2025-02-08 15:00:31,821: Error running WSGI application
2025-02-08 15:00:31,822: NameError: name 'get_key' is not defined
2025-02-08 15:00:31,822:   File "/var/www/akrabato_pythonanywhere_com_wsgi.py", line 25, in <module>
2025-02-08 15:00:31,822:     application = get_wsgi_application()
2025-02-08 15:00:31,822: 
2025-02-08 15:00:31,823:   File "/home/akrabato/.virtualenvs/djangovet_env/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2025-02-08 15:00:31,823:     django.setup(set_prefix=False)
2025-02-08 15:00:31,823: 
2025-02-08 15:00:31,823:   File "/home/akrabato/.virtualenvs/djangovet_env/lib/python3.10/site-packages/django/__init__.py", line 19, in setup
2025-02-08 15:00:31,823:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2025-02-08 15:00:31,824: 
2025-02-08 15:00:31,824:   File "/home/akrabato/.virtualenvs/djangovet_env/lib/python3.10/site-packages/django/conf/__init__.py", line 82, in __getattr__
2025-02-08 15:00:31,824:     self._setup(name)
2025-02-08 15:00:31,824: 
2025-02-08 15:00:31,824:   File "/home/akrabato/.virtualenvs/djangovet_env/lib/python3.10/site-packages/django/conf/__init__.py", line 69, in _setup
2025-02-08 15:00:31,825:     self._wrapped = Settings(settings_module)
2025-02-08 15:00:31,825: 
2025-02-08 15:00:31,825:   File "/home/akrabato/.virtualenvs/djangovet_env/lib/python3.10/site-packages/django/conf/__init__.py", line 170, in __init__
2025-02-08 15:00:31,825:     mod = importlib.import_module(self.SETTINGS_MODULE)
2025-02-08 15:00:31,825: 
2025-02-08 15:00:31,826:   File "/home/akrabato/djangovet/djangovet/settings.py", line 94, in <module>
2025-02-08 15:00:31,826:     'USER': get_key("DBUSER"),
2025-02-08 15:00:31,826: ***************************************************
2025-02-08 15:00:31,826: If you're seeing an import error and don't know why,
2025-02-08 15:00:31,826: we have a dedicated help page to help you debug: 
2025-02-08 15:00:31,827: https://help.pythonanywhere.com/pages/DebuggingImportError/

But I have a defiinition of getKey function in line 25:

def getKey(setting, project_keys=project_keys):
    try:
        return project_keys[setting]
    except KeyError:
        errorMessage = "Set the {} env var".format(setting)
        raise ImproperlyConfigured(errorMessage)

So, I don’t understand why I got this error in line 94:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
        'USER': get_key("DBUSER"),
        'PASSWORD': get_key("DBPASSWORD"),
        'HOST': get_key("DBHOST"),
        'PORT': '5432',
    }
}

Could you, please, provide full settings.py file!

Just read that this function is used only if using PostgreSQL/PostGIS.