0

I am having a hard time deploying my finished Django project to PythonAnywhere. It looks like it has to do with my Django secret key, which I never seem to handle correctly. The secret key is exposed in settings.py, which I know is not advised, but whenever I put it into an environment variable PythonAnywhere throws an error that it can't find it. Can anyone help me configure my settings.py + "pythonanywhere_com_wsgi.py" file to correctly deploy my application?

Github link to project: https://github.com/mollycg/django-portfolio

PythonAnywhere error log:

Error running WSGI application
2020-12-03 22:32:51,873: decouple.UndefinedValueError: SECRET_KEY not found. Declare it as envvar or define a default value.
2020-12-03 22:32:51,873:   File "/var/www/mollycg_pythonanywhere_com_wsgi.py", line 12, in <module>
2020-12-03 22:32:51,873:     application = StaticFilesHandler(get_wsgi_application())
2020-12-03 22:32:51,873: 
2020-12-03 22:32:51,873:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-12-03 22:32:51,873:     django.setup(set_prefix=False)
2020-12-03 22:32:51,874: 
2020-12-03 22:32:51,874:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2020-12-03 22:32:51,874:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2020-12-03 22:32:51,874: 
2020-12-03 22:32:51,874:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
2020-12-03 22:32:51,874:     self._setup(name)
2020-12-03 22:32:51,874: 
2020-12-03 22:32:51,874:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
2020-12-03 22:32:51,874:     self._wrapped = Settings(settings_module)
2020-12-03 22:32:51,874: 
2020-12-03 22:32:51,874:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/django/conf/__init__.py", line 142, in __init__
2020-12-03 22:32:51,875:     mod = importlib.import_module(self.SETTINGS_MODULE)
2020-12-03 22:32:51,875: 
2020-12-03 22:32:51,875:   File "./magic_django/settings.py", line 21, in <module>
2020-12-03 22:32:51,875:     SECRET_KEY = config('SECRET_KEY')
2020-12-03 22:32:51,875: 
2020-12-03 22:32:51,875:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/decouple.py", line 199, in __call__
2020-12-03 22:32:51,875:     return self.config(*args, **kwargs)
2020-12-03 22:32:51,875: 
2020-12-03 22:32:51,875:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/decouple.py", line 83, in __call__
2020-12-03 22:32:51,875:     return self.get(*args, **kwargs)
2020-12-03 22:32:51,875: 
2020-12-03 22:32:51,875:   File "/home/mollycg/.virtualenvs/mgdp-venv/lib/python3.8/site-packages/decouple.py", line 68, in get
2020-12-03 22:32:51,876:     raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
2020-12-03 22:32:51,876: ***************************************************
2020-12-03 22:32:51,876: If you're seeing an import error and don't know why,
2020-12-03 22:32:51,876: we have a dedicated help page to help you debug: 
2020-12-03 22:32:51,876: https://help.pythonanywhere.com/pages/DebuggingImportError/
2020-12-03 22:32:51,876: ***************************************************

The error log also references a package I tried to use for the SECRET_KEY, python-decouple, which I have since stopped using in settings.py. If I need to use it, that works too, but it wasn't working when I tried earlier.

PythonAnywhere WSGI file:

import os
import sys

path = os.path.expanduser('~/mollycg/django-portfolio')
if path not in sys.path:
    sys.path.insert(0, path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'magic_django.settings'

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())
2
  • 1
    You need to provide that variable in some way. python-decouple can use ini and .env files, so you can have a look at using one of those to provide the variable to your web app. Commented Dec 4, 2020 at 12:18
  • I got it to work! Thank you for the input. Commented Dec 4, 2020 at 15:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.