Django WSGI application could not be loaded - django

I found an error while re-running the django application and there are some solutions on stackoverflow,but it is not working for me .
django.core.exceptions.ImproperlyConfigured: WSGI application
'myapp.wsgi.application' could not be loaded; Error importing module.
Also checked Django Middle-Ware,All are installed and Properly Configured.
Following are the Middle-Ware i am using
'django_session_timeout.middleware.SessionTimeoutMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
MY DJANGO APPLICATION: myapp
WSGI_APPLICATION = 'myapp.wsgi.application'
Django Version: 2.1.7
Python : 3.7.1
WSGI.PY
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
application = get_wsgi_application()

Related

Django + Mongoengine - settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details

And here I am again.
I am trying to connect my app to MongoDB as I want to implement a non-rel database. The application works fine with SQL3Lite and I was also able to use Djongo. Yet, I am planning to use MongoEngine models and therefore I am trying to use it as DB Engine.
However, for whatever reason I receive an error settings.
"DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details."
Here is what I did:
django-admin startproject projectname
python manage.py startapp appname
Models.py:
from django.db import models
from django.db.models.fields.related import ForeignKey
from django.db.models.query import EmptyQuerySet
from django.contrib.auth.models import User,AbstractUser, UserManager
import datetime
import mongoengine
# Create your models here.
class Project(mongoengine.Document):
projectName = mongoengine.StringField()
Settings.py
import os
from pathlib import Path
import mongoengine
mongoengine.connect(db="testdatabase", host="mongodb+srv://<Username>:<Password>#cluster0.qspqt0a.mongodb.net/?retryWrites=true&w=majority")
# 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 https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = secretKey
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api'
]
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 = 'prodash.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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 = 'prodash.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy'
}
}
At this point I receive always the same error if:
I create a superuser
I migrate data
I login into the app
(All of the above work with SQL3Lite)
My pip list is:
asgiref 3.5.2
certifi 2022.6.15
charset-normalizer 2.1.1
Django 4.1
djangorestframework 3.13.1
dnspython 2.2.1
idna 3.3
mongoengine 0.24.2
pip 22.2.2
pymongo 4.2.0
pytz 2022.2.1
requests 2.28.1
setuptools 63.4.3
sqlparse 0.4.2
urllib3 1.26.12
It might be a typo, but I can't figure out what I am doing wrong. Please, help.

Pythonanywhere - something went wrong - error running wsgi - modulenotfound error

I am new to django and I am doing a coursera course with little applications deployed on pythonanywhere, which has worked well so far. No I am stuck, because does not load at all. Pythonanywhere says that Something went wrong.
This is the error log from pythonanywhere.
Error running WSGI application
ModuleNotFoundError: No module named 'django_extensions'
This is my WSGI file
"""
WSGI config for mysite 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/3.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
This is my settings.py file, not the entire one but the installed apps:
DEBUG = True
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.humanize',
'ads.apps.AdsConfig',
# Extensions - installed with pip3 / requirements.txt
'django_extensions',
'crispy_forms',
'rest_framework',
'social_django',
'taggit',
'home.apps.HomeConfig',
]
This is my urls.py, not the entire one, but the crucial part.
import os
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.views.static import serve
urlpatterns = [
path('', include('home.urls')), # Change to ads.urls
path('ads/', include('ads.urls')),
path('admin/', admin.site.urls), # Keep
path('accounts/', include('django.contrib.auth.urls')), # Keep
# url(r'^oauth/', include('social_django.urls', namespace='social')), # Keep
]
I checked django version, requirements are installed. I double checked with pip list.
Running python3 manage.py check has no errors. I can do all mirgrations without errors. I don't know anymore where to look for the problem.
I already did 3 or 4 of these mini applications and I did not have this problem. wsgi points to mysite.settings and the settings.py has django_extensions as INSTALLES APPS.
What is the problem? Please help
There are multiple Python versions installed on PythonAnywhere. Make sure that you installed the required module for the same Python environment that your web app is being run by. For example, if your web app is set to be run by Python 3.8, check if you installed the module with pip3.8 django_extensions --user command; if you are using a virtual environment, make sure it's set on the Web page and that you installed the module inside of that venv.

Django Static Files in Production Using Whitenoise

Firstly apologies for the length of this.
I have a django project running on Centos6 and Apache using a C-Panel plugin to install Django and I am trying to serve the static files in production.
My project uses Django 1.9 and I am trying to use Whitenoise to serve my static files.
My settings.py contains the following:
STATICFILES_STORAGE ='whitenoise.django.GzipManifestStaticFilesStorage'
STATIC_URL = '/static/'
STATIC_ROOT = STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'common-static'),
And this is my MIDDLEWARE_CLASSES
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
)
This is the wsgi.py for my django app
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
And this is the one generated by the plugin:
import os
import sys
import site
vepath = '/home/mysite/virtualenv3.5/lib/python3.5/site-packages'
prev_sys_path = list(sys.path)
site.addsitedir(vepath)
sys.path.append('/home/mysite/djangosites/mysite')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE","mysite.settings"
application = get_wsgi_application()
I have DEBUG = False in my settings.py.
When I run ./manage.py collectstatic the files are collected and if I run the server with 0.0.0.0:8000 the static files are found.
I tried add the following to both wsgi.py files but to no avail:
from whitenoise.django importDjangoWhiteNoise
application = DjangoWhiteNoise(application)
Any advice on how to solve this would be appreciated.
It's a late response but it might help other people searching for an answer.
You do NOT have to include 'django.middleware.security.SecurityMiddleware' when you're using 'whitenoise.middleware.WhiteNoiseMiddleware'.
Also, you don't need to make any changes in wsgi.py. Adding the middleware will take care of everything for you.

Django debug toolbar crashes when deployed on AWS Elastic Beanstalk

I am running a Django app on AWS Elastic Beanstalk but Django debug toolbar is causing the following error:
Traceback (most recent call last):
File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 235, in get_response
response = middleware_method(request, response)
File "/opt/python/run/venv/lib/python3.4/site-packages/debug_toolbar/middleware.py", line 123, in process_response
response.content = insert_before.join(bits)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/http/response.py", line 315, in content
value = self.make_bytes(value)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/http/response.py", line 235, in make_bytes
return bytes(value.encode(self.charset))
UnicodeEncodeError: 'utf-8' codec can't encode character '\\udcc3' in position 142917: surrogates not allowed
I did not have this problem locally or when running my django app on an AWS EC2 instance where I setup nginx and gunicorn myself. Something about Elastic Beanstalk? Maybe that it's apache?
Some details about my configuration:
Versions
Same locally as well as on AWS EBS
Python 3.4.3
Django 1.9.7
Django Toolbar 1.5
Django Settings
The following are pieces of my django settings where I activate the toolbar
INSTALLED_APPS = [
'compare.apps.CompareConfig',
'search.apps.SearchConfig',
'profile.apps.ProfileConfig',
'common.apps.CommonConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_toolbar',
]
(I tried moving django_toolbar further up in the list of installed apps but it didn't help)
MIDDLEWARE_CLASSES = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
DEBUG_TOOLBAR_PATCH_SETTINGS = False
def custom_show_toolbar(request):
return True # Always show toolbar, for example purposes only.
DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar,
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
And in my root url conf file I have
urlpatterns += [url(r'^__debug__/', include(debug_toolbar.urls))]
There was some other SO (can't find link now) that mentioned something about adding utf-8 to one of the language or USE_*** parameters but I tried it and didn't help - but perhaps I didn't do it correctly also.
I really love the Django debug toolbar, would greatly appreciate being able to resolve this.
Any ideas?
Thanks!

Django toolbar not displaying properly

I am using django 1.6.4 and django toolbar version django-debug-toolbar==1.2.
I do get the toolbar on the side but when I click on any of the menu items, it shows me the html elements from my home page within the toolbar overlay. I have done collectstatic after installing django toolbar. HTML generated is not valid but issue seems to be from the css and scripts tag from django toolbar.
Any help will be much appreciated.
Thanks
Updated:
urls.py entry
if settings.DEBUG:
import debug_toolbar
urlpatterns += patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
)
settings.py
DEBUG = True
MIDDLEWARE_CLASSES = (
'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',
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
THIRD_PARTY_APPS = (
'suit',
'debug_toolbar',
'south',
'crispy_forms',
'haystack',
'taggit',
)