So I want to deploy my website on pythonanywhere. I created virtualenv, installed Django and all the apps that I need for my website. I created new app and connected it with my project that I cloned from GitHub. Here is a screenshot of the setting in my web app:
My project structure looks like this:
My settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ADMIN_ENABLED = False
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'taggit',
'blog',
'widget_tweaks',
'ckeditor',
'ckeditor_uploader',
'user_account',
'captcha',
'mptt',
]
SITE_ID = 1
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'blog_project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'blog_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
"""
# Use this for testing on local pc
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
"""
#Use this for Deployment
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'falca94$blogezzdatabase',
'USER': 'falca94',
'PASSWORD': os.environ.get('DATABASE_PASS'),
'HOST': 'falca94.mysql.pythonanywhere-services.com',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 5,
},
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Django-allauth
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Belgrade'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
#'/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media_cdn')
#slanje maila
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'blogezz.master@gmail.com'
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Custom allauth settings
# Use email as the primary identifier
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
# Make email verification mandatory to avoid junk email accounts
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
# Eliminate need to provide username, as it's a very old practice
ACCOUNT_USERNAME_REQUIRED = False
# Redirect user after logout
ACCOUNT_LOGOUT_REDIRECT_URL = "/accounts/login"
# Our personal signup form
ACCOUNT_SIGNUP_FORM_CLASS = 'user_account.forms.SignupForm'
ACCOUNT_ADAPTER = 'user_account.adapter.AccountAdapter'
# To not show the logout page after the user logs out, set ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_LOGOUT_ON_GET = False
# Configuration for DJANGO-CKEDITOR
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_RESTRICT_BY_USER = True
CKEDITOR_BROWSE_SHOW_DIRS = True
CKEDITOR_RESTRICT_BY_DATE = True
CKEDITOR_JQUERY_URL = '//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'
CKEDITOR_CONFIGS = {
'default': {
'skin': 'moono',
'height': 800,
},
}
And my /var/www/falca94_pythonanywhere_com_wsgi.py
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/falca94/mysite/mysite/settings.py'
## and your manage.py is is at '/home/falca94/mysite/manage.py'
path = '/home/falca94/blog_project'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'blog_project.settings'
os.environ['SECRET_KEY'] = 'mysecretkey'
os.environ['EMAIL_PASS'] = 'emailpassword'
os.environ['DATABASE_PASS'] = 'databasepass'
# then, for django >=1.5:
from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())
## or, for older django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()
I don't understand why I am getting this error:
Error running WSGI application
2017-05-05 11:33:43,246 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
2017-05-05 11:33:43,246 : File "/var/www/falca94_pythonanywhere_com_wsgi.py", line 12, in <module>
2017-05-05 11:33:43,247 : application = StaticFilesHandler(get_wsgi_application())
2017-05-05 11:33:43,247 :
2017-05-05 11:33:43,247 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2017-05-05 11:33:43,247 : django.setup(set_prefix=False)
2017-05-05 11:33:43,247 :
2017-05-05 11:33:43,247 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/__init__.py", line 22, in setup
2017-05-05 11:33:43,247 : configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2017-05-05 11:33:43,247 :
2017-05-05 11:33:43,248 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
2017-05-05 11:33:43,248 : self._setup(name)
2017-05-05 11:33:43,248 :
2017-05-05 11:33:43,248 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
2017-05-05 11:33:43,248 : self._wrapped = Settings(settings_module)
2017-05-05 11:33:43,248 :
2017-05-05 11:33:43,248 : File "/home/falca94/blog_project/blog_project_env/lib/python3.6/site-packages/django/conf/__init__.py", line 129, in __init__
2017-05-05 11:33:43,248 : raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
Your settings.py
tries to read the SECRET_KEY
from a environment variable:
SECRET_KEY = os.environ.get('SECRET_KEY')
The error is indicating that this environment variable is not set in the environment you are trying to run your Django application.
In the case of Python Anywhere you can read about this very case in their documentation: https://help.pythonanywhere.com/pages/environment-variables-for-web-apps