I have a Django project called Veganet that I am trying to run, but when I run my main script vega_flask_run.py it gives me an improperly configured error. The source of the error is coming from a script named models.py, more specifically line 2 where I am declaring from django.contrib.auth.models import User. I have tried to use a few posts to solve my problem including:
I have tried solutions like changing the INSTALLED_APPS variable in my projects settings.py and changing the DATABASES variable inside settings.py, as well as using Windows Powershell to execute python manage.py shell, django-admin.py shell --settings=mysite.settings, and then setting the DJANGO_SETTINGS_MODULE variable to point to my projects settings and lastly using
from django.core.management import setup_environ
from veganet import settings
setup_environ(settings)
here is the error in it's entirety:
Exception has occurred: ImproperlyConfigured
Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
File "C:\Users\trevo\OneDrive\Desktop\veganet\profiles\models.py", line 2, in <module>
from django.contrib.auth.models import User
File "C:\Users\trevo\OneDrive\Desktop\veganet\profiles\forms.py", line 2, in <module>
from .models import ExperimentContext, Profile
File "C:\Users\trevo\OneDrive\Desktop\veganet\vega_ai\VegaMain.py", line 25, in <module>
from profiles.forms import ProfileModelForm, ExperimentModelForm
File "C:\Users\trevo\OneDrive\Desktop\veganet\vega_flask_run.py", line 5, in <module>
from vega_ai.VegaMain import app as frontend
Here is my simplified file structure:
veganet (main folder)
-profiles (folder)
...
models.py (script that gives me the error)
signals.py
urls.py
utils.py
views.py
-vega_ai (folder)
VegaMain.py
...
-vega_net (folder)
...
asgi.py
settings.py
urls.py
views.py
wsgi.py
db.sqlite3
manage.py
vega_flask_run.py (where I am running the project)
my settings.py file:
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
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/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-p8!i$5eqn)l(f0(n##1yntb^#ctfm3u*)j9xrjy^(1n9s&jdsa'
# 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',
'posts',
'profiles',
'veganet',
]
LOGIN_URL = '/admin/'
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 = 'veganet.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["C:/Users/trevo/OneDrive/Desktop/veganet/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 = 'veganet.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_project')
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
MEDIA_URL = '/media/'
MEDIA_ROOT = 'C:/Users/trevo/OneDrive/Desktop/veganet/static_cdn/media_root'
my wsgi.py script:
import os
from django.conf import settings
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'veganet.settings')
application = get_wsgi_application()
models.py
from django.db import models
from django.contrib.auth.models import User
from django.dispatch.dispatcher import receiver
from .utils import get_random_code
from django.template.defaultfilters import slugify
import subprocess
...
vega_flask_run.py
from email.mime import application
from flask import Flask, render_template
from werkzeug.serving import run_simple
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from vega_ai.VegaMain import app as frontend
from manage import app as backend
from werkzeug.exceptions import NotFound
app = Flask(__name__, template_folder="C:/Users/trevo/OneDrive/Desktop/veganet/profiles/templates/profiles", static_folder="C:/Users/trevo/OneDrive/Desktop/veganet/static_project")
app.wsgi_app = DispatcherMiddleware(frontend, {
'/app2':app,
'/app3':backend
})
if __name__ == '__main__':
app.run(debug=True,port=8080,use_reloader=False)
What exactly am I missing here? Thank you
If this is Django, and not flask as the vega_flask_run.py name implies then you need to run python manage.py runserver to start the application.
This might be a duplicate of Django ImproperlyConfigured for import User.
Also, models.py is not a script your run in django in the manner I think you're saying. It is where the models you define, the classes, for your project are located.
I may be the one missing something. Maybe you can edit your question with the vega_flask_run.py file. Perhaps you're using it in a way that I'm just not familiar with.
Otherwise, I suggest you start with https://docs.djangoproject.com/en/4.0/intro/tutorial01/. I mean it looks like you've already started the project correctly since you have the django files there. Maybe it's just a matter of how you start them.
Edit
Yes, now I can see that you're trying to run a djagno app as if it were a flask app, app = Flask(__name__, .... Despite both Flask and Django being python frameworks, they are run very differently.
Related
I am not able to display media files on Cpanel shared hosting Django web app. I receive a Error 404 URL Not Found whenever I try to access the media file.
I have specified + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in my urls.py file.
This error only occurs whenever DEBUG = False. I am using whitenoise to serve the static files without any issue it is only the media directory that causes the 404 web error.
settings.py
"""
Django settings for app project.
Generated by 'django-admin startproject' using Django 3.2.9.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path
# 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/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
# Replace with your secret key. Secret key is set as an env variable in Python
# Web Application Page
SECRET_KEY = os.environ['SECRET_KEY']
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*', 'subdomain.myapp.com']
# Application definition
INSTALLED_APPS = [
'myapp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'app.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 = 'app.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
# Static files are served via whitenoise
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static/"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
passenger_wsgi.py
import os
import sys
#########Accessing the App's Default WSGI##############
import app.wsgi
application = app.wsgi.application
######## PASSENGER PATH INFO-FIX INITIALISATIONS#######
cwd = os.getcwd()
sys.path.append(cwd)
#sys.path.append(os.getcwd())
sys.path.append(cwd + '/app') #You must add your project here
# Set this to your root
SCRIPT_NAME = os.getcwd()
######## MIDDLEWARE CLASS TO FIX PASSENGER'S URI ISSUES #############
class PassengerPathInfoFix(object):
"""
Sets PATH_INFO from REQUEST_URI since Passenger doesn't provide it.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
#IF YOU ARE IN PYTHON 2.7 USE: from urllib import unquote
from urllib.parse import unquote
environ['SCRIPT_NAME'] = SCRIPT_NAME
request_uri = unquote(environ['REQUEST_URI'])
script_name = unquote(environ.get('SCRIPT_NAME', ''))
offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
return self.app(environ, start_response)
###########the redirecting Middleware
application = PassengerPathInfoFix(application)
I am using Python v3.8.6, Django=2.1 and CPanel Shared Hosting via NameCheap.
I know it's recommended to have a webserver to store and serve media files in Production Environment but I am unable to edit the Apache httpd.conf file as mentioned in the Django documentation.
I've got a Django app that uses graphene to implement GraphQL and I've got everything setup and working but I now have an error in the console which has popped up suddenly and although it doesn't break anything, at least from as far as what I can tell, it does keep showing up in the console and I'd like to fix it.
I'm quite new to Django so I'm not able to figure out where this is coming from. It looks like it's coming from the channels package.
This is the error in its entirety that happens immediately after the server runs and then again after every request is made.
Django version 3.2.3, using settings 'shuddhi.settings'
Starting ASGI/Channels version 3.0.3 development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /graphql/ [172.28.0.1:60078]
Exception inside application: 'NoneType' object has no attribute 'replace'
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/channels/staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/usr/local/lib/python3.8/site-packages/channels/security/websocket.py", line 35, in __call__
if self.valid_origin(parsed_origin):
File "/usr/local/lib/python3.8/site-packages/channels/security/websocket.py", line 54, in valid_origin
return self.validate_origin(parsed_origin)
File "/usr/local/lib/python3.8/site-packages/channels/security/websocket.py", line 73, in validate_origin
return any(
File "/usr/local/lib/python3.8/site-packages/channels/security/websocket.py", line 74, in <genexpr>
pattern == "*" or self.match_allowed_origin(parsed_origin, pattern)
File "/usr/local/lib/python3.8/site-packages/channels/security/websocket.py", line 98, in match_allowed_origin
parsed_pattern = urlparse(pattern.lower(), scheme=None)
File "/usr/local/lib/python3.8/urllib/parse.py", line 376, in urlparse
splitresult = urlsplit(url, scheme, allow_fragments)
File "/usr/local/lib/python3.8/urllib/parse.py", line 433, in urlsplit
scheme = _remove_unsafe_bytes_from_url(scheme)
File "/usr/local/lib/python3.8/urllib/parse.py", line 422, in _remove_unsafe_bytes_from_url
url = url.replace(b, "")
AttributeError: 'NoneType' object has no attribute 'replace'
This is my Settings.py file:-
"""
Django settings for main_project project.
Generated by 'django-admin startproject' using Django 3.2.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
import os
import dj_database_url
from environ import Env
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# BASE_DIR = Path(__file__).resolve().parent.parent
# This is to import the environment variables in the .env file
env = Env()
env.read_env(os.path.join(BASE_DIR, '.env')) # This reads the environment variables from the .env file
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# # # # # # # # #
# Loading all environemtn Variables
# # # # # # # #
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('DJANGO_SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DJANGO_DEBUG', default=False)
# Authorized origins
ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS')
# Whether or not requests from other origins are allowed
CORS_ORIGIN_ALLOW_ALL = env.bool('DJANGO_CORS_ORIGIN_ALLOW_ALL')
# Twilio Sendgrid API key
SENDGRID_API_KEY = env('SENDGRID_API_KEY')
# setting default email for sending email through sendgrid
DEFAULT_FROM_EMAIL = env('FROM_EMAIL_ID')
SENDGRID_SANDBOX_MODE_IN_DEBUG= env.bool('SENDGRID_SANDBOX_MODE_IN_DEBUG')
# Application definition
# Sendgrid Mail Settings
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
INSTALLED_APPS = [
'corsheaders',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name',
'graphene_django',
'graphql_jwt.refresh_token.apps.RefreshTokenConfig',
'graphql_auth',
'rest_framework',
'django_filters',
'channels'
]
GRAPHENE = {
'SCHEMA': 'main_project.schema.schema',
'MIDDLEWARE': [
'graphql_jwt.middleware.JSONWebTokenMiddleware',
],
}
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'common.utils.UpdateLastActivityMiddleware'
]
AUTHENTICATION_BACKENDS = [
'graphql_auth.backends.GraphQLAuthBackend',
'django.contrib.auth.backends.ModelBackend',
]
GRAPHQL_AUTH = {
"ALLOW_LOGIN_NOT_VERIFIED": False
}
GRAPHQL_JWT = {
"JWT_ALLOW_ANY_CLASSES": [
"graphql_auth.mutations.Register",
"graphql_auth.mutations.VerifyAccount",
"graphql_auth.mutations.ResendActivationEmail",
"graphql_auth.mutations.SendPasswordResetEmail",
"graphql_auth.mutations.PasswordReset",
"graphql_auth.mutations.ObtainJSONWebToken",
"graphql_auth.mutations.VerifyToken",
"graphql_auth.mutations.RefreshToken",
"graphql_auth.mutations.RevokeToken",
],
'JWT_PAYLOAD_HANDLER': 'common.utils.jwt_payload',
"JWT_VERIFY_EXPIRATION": True,
"JWT_LONG_RUNNING_REFRESH_TOKEN": True
}
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ROOT_URLCONF = 'main_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 = 'main_project.wsgi.application'
ASGI_APPLICATION = 'main_project.router.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_projectdb',
'USER': 'main_projectadmin',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '5432',
}
}
DATABASE_URL = os.environ.get('DATABASE_URL')
db_from_env = dj_database_url.config(default=DATABASE_URL, conn_max_age=500, ssl_require=True)
DATABASES['default'].update(db_from_env)
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("redis", 6379)],
},
},
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# This is here because we are using a custom User model
# https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#substituting-a-custom-user-model
AUTH_USER_MODEL = "app_name.User"
urls.py in the main_project folder:-
from django.contrib import admin
from django.urls import include, path
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('app_name.urls')),
path('admin/', admin.site.urls),
path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True))),
]
if settings.DEBUG:
urlpatterns += (
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
)
urls.py in the app:-
from . import views
from django.urls import path
from .views import *
urlpatterns = [
path('', views.index, name='index'),
]
Router.py file where I've specified stuff needed for subscriptions:-
from base64 import decode
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application
from django.urls import path
from .schema import MyGraphqlWsConsumer
from django.contrib.auth.models import AnonymousUser
from django.contrib.auth import get_user_model
from channels.db import database_sync_to_async
from channels.middleware import BaseMiddleware
import jwt
from .settings import SECRET_KEY
# from user.models import Token
#database_sync_to_async
def get_user(token_key):
try:
decodedPayload = jwt.decode(
token_key, key=SECRET_KEY, algorithms=['HS256'])
user_id = decodedPayload.get('sub')
User = get_user_model()
user = User.objects.get(pk=user_id)
return user
except Exception as e:
return AnonymousUser()
# This is to enable authentication via websockets
# Source - https://stackoverflow.com/a/65437244/7981162
class TokenAuthMiddleware(BaseMiddleware):
def __init__(self, inner):
self.inner = inner
async def __call__(self, scope, receive, send):
query = dict((x.split("=")
for x in scope["query_string"].decode().split("&")))
token_key = query.get("token")
print('token from subscription request =>', token_key)
scope["user"] = await get_user(token_key)
print('user subscribing ', scope["user"])
scope["session"] = scope["user"] if scope["user"] else None
return await super().__call__(scope, receive, send)
application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AllowedHostsOriginValidator(TokenAuthMiddleware(
URLRouter(
[path("graphql/", MyGraphqlWsConsumer.as_asgi())]
)
)),
}
)
Not sure what else is necessary here, but this is all I can think of. It would be great to know how to troubleshoot and get rid of this exception.
Update:-
As I continue to troubleshoot this, I am discovering that I'm starting to see this after I moved the ALLOWED_HOSTS variable to the env file and if I set ALLOWED_HOSTS = ['*'] in the settings.py file, the error goes away. And it shows up only when the subscription from the UI happens. I definitely want to have the ALLOWED_HOSTS getting the value from the environment variable because it's going to be different for prod and dev and it will need to be set from the env variables.
Right now the .env file has this - DJANGO_ALLOWED_HOSTS=localhost,0.0.0.0 and it results in the ALLOWED_HOSTS being rendered as ['localhost', '0.0.0.0']
TlDR - Upgrade the channels package to 3.0.4.
Details:-
I've narrowed down the issue to it being caused by ALLOWED_HOSTS having anything other than ['*']. So if I had a specific list of allowed domains such as ['localhost', '0.0.0.0'], it throws an exception and it actually prevents subscriptions from working.
The culprit is the use of 'AllowedHostsOriginValidator' of the channels package that somehow breaks when using Python 3.8. The issue is documented here
Fix has been added to version 3.0.4 of the channels package. Just upgrade the package it should work just fine.
This might probably be a duplicated question, but I can't find a post to answer my questions yet. Any post that is similar to this may help is appreciated.
I tried to host my Django app using heroku.com
git add .
git commit -m "(commit_name)"
git push heroku master
When I tried to test the website (/questions/1), the website shows an Error 500 (Internal Error).
First it shows a ProgrammingError: relation does not exist.
After that I did $ heroku run python manage.py migrate try to solve the problem. The original error disappeared, but instead this happened:
2020-08-29T11:05:42.668070+00:00 app[web.1]: Internal Server Error: /questions/1
2020-08-29T11:05:42.668070+00:00 app[web.1]:
2020-08-29T11:05:42.668070+00:00 app[web.1]: DoesNotExist at /questions/1
2020-08-29T11:05:42.668071+00:00 app[web.1]: Question matching query does not exist.
Settings.py:
import django_heroku
from pathlib import Path
import os
#--------------------(ommited)--------------------#
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = 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',
'myapp',
]
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 = 'myweb.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 = 'myweb.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'zh-Hant'
TIME_ZONE = 'Asia/Taipei'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Activate Django-Heroku.
django_heroku.settings(locals())
Models.py:
from django.db import models
from django.utils import timezone
import json
# Create your models here.
class Code(models.Model):
code = models.TextField()
number = models.IntegerField()
class Question(models.Model):
title = models.TextField()
text = models.TextField()
judges = models.TextField()
number = models.IntegerField()
class Meta:
ordering = ['number']
def set_judges(self, x):
self.judges = json.dumps(x)
def get_judges(self):
return json.loads(self.judges)
wsgi.py:
import os
from dj_static import Cling
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myweb.settings')
application = Cling(get_wsgi_application())
Any suggestions or things I should try? Thank you.
[EDIT]: It seems that the database is empty now so it cause the error. But when I run the same file in my computer as I git push to heroku, the database isn't empty and it works fine.
According to your settings file, you are using sqlite as the database, and you can't use it in Heroku.
Heroku uses an an ephemeral filesystem.
You can write to it, and you can read from it, but the contents will
be cleared periodically. If you were to use SQLite on Heroku, you
would lose your entire database at least once every 24 hours.
That's why it works locally but not on Heroku, you need to use another database engine like postgresql for example.
Learn more about it at: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted
I’m new to Python and Django, but have decided to make my own wedding website based on https://www.placecard.me/blog/django-wedding-website/. The only major difference I want to make is to change the the email communication to SMS. I came across this https://github.com/CleverProgrammer/CP-Twilio-Python-Text-App texting app.
I incorporated the texting app into a Django project to test and attempt to send a text message to all guests in the database. I’m running Python 3.6.5 and Django 2.0.5
I have the following directory structure for my Django project.
I have the following code:
settings.py
import os
enter code here`# 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/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'twilio',
'sms_send',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
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 = 'WebsiteSMS.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'wsgi.application'
WSGI_APPLICATION = 'WebsiteSMS.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-
validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME':
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME':
'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "WebsiteSMS.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
send_sms.py:
from twilio.rest import Client
from credentials import account_sid, auth_token, my_cell, my_twilio
client = Client(account_sid, auth_token)
my_msg = "Test message"
message = client.messages.create(to=my_cell, from_=my_twilio, body=my_msg)
I then run python
sms_send\send_sms.py.
This sends a SMS to my phone.
I then add the following to try and send the same message to both guests already in the database. I did all the migrations.
admin.py
from .models import SmsUser
from django.contrib import admin
class SmsUserAdmin(admin.ModelAdmin):
list_display = ('name', 'number')
search_fields = ['name', 'number']
admin.site.register(SmsUser, SmsUserAdmin)
models.py
from django.db import models
class SmsUser(models.Model):
name = models.TextField(null=True, blank=True)
number = models.CharField(max_length=13, null=True, blank=True)
def __str__(self):
return ' {}'.format(self.name)
def __str__(self):
return ' {}'.format(self.number)
And change send_sms.py to the following:
from twilio.rest import Client
from credentials import account_sid, auth_token, my_cell, my_twilio
from models import SmsUser
client = Client(account_sid, auth_token)
recipients = SmsUser.objects.all()
for recipient in recipients:
client.messages.create(body='Sample text', to=recipient.number,
from_=my_twilio)
When I run python sms_send\send_sms.py again I get the following error:
PS C:\Garbage\Python\Django\WebsiteSMS> python sms_send\send_sms.py
Traceback (most recent call last):
File "sms_send\send_sms.py", line 3, in
from models import SmsUser
File "C:\Garbage\Python\Django\WebsiteSMS\sms_send\models.py", line 4, in
class SmsUser(models.Model):
File "C:\Users\Gary.HIBISCUS_PC\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\db\models\base.py", line 100, in new
app_config = apps.get_containing_app_config(module)
File "C:\Users\Gary.HIBISCUS_PC\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\apps\registry.py", line 244, in
get_containing_app_config
self.check_apps_ready()
File "C:\Users\Gary.HIBISCUS_PC\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\django\apps\registry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I’ve tried the suggested answers but have not been able to get this working. Everything seems fine until I add from models import SMSUser in send_sms.py
I hope someone can identify my problem and point me in the right direction.
You are attempting to run a python file individually which I think causes your app to fail. It is attempting to load SmsUser which lives within your django project, but can not be reached when calling the file with python <dir>/<file>.py.
If you want to run this file within your django project and being able to access your models, database, etc as a command, you could use django custom management commands
Quick untested example:
# Django holds a specific management commands path like eg.:
# send_sms.management.commands.send_pending_sms_messages
# which would be as a specific path send_sms/management/commands/send_pending_sms_messages.py
from django.core.management.base import BaseCommand
from twilio.rest import Client
from credentials import account_sid, auth_token, my_cell, my_twilio
from models import SmsUser
class Command(BaseCommand):
help = 'Send pending SMS messages'
def handle(self, *args, **options):
client = Client(account_sid, auth_token)
recipients = SmsUser.objects.all()
for recipient in recipients:
client.messages.create(body='Sample text', to=recipient.number,
from_=my_twilio)
If all set up properly, you can now use the ./manage.py as your command runner within your django project like
./manage.py send_pending_sms_messages
I am attempting to add background workers using Celery to automatically run scripts each day to update the information my website provides. I've integrated Celery with Django and Heroku, and I can import the module and use the function, but it freezes when I use the add.delay()command until I press Ctrl+C to cancel the command. I am using celery 4.1 Here is how I run the commands:
heroku ps:scale worker=1
heroku run python
>>>from Buylist.tasks import *
>>>add(2,3)
>>>5
>>>add.delay(2,3)
#-- Freezes until I press Control+C
If you could help me figure out where my settings are misconfigured, that would be great. I'm testing atm. the tasks.py is the sample code to get a working example, and then I'll move on to figuring out CELERY_BEAT settings
project/celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
#app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
Buylist/tasks # Buylist is the single app in my root directory
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task
from celery import Celery
from celery.schedules import crontab
#shared_task
def test(arg):
print(arg)
#shared_task
def add(x, y):
return x + y
#shared_task
def mul(x, y):
return x * y
#shared_task
def xsum(numbers):
return sum(numbers)
project/settings.py
from __future__ import absolute_import, unicode_literals
import dj_database_url
"""
Django settings for project project.
Generated by 'django-admin startproject' using Django 2.0.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
import django_heroku
# 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/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
#Read secret key from a file
SECRET_KEY = 'KEY'
# SECURITY WARNING: don't run with debug turned on in production!
#DEBUG = True
DEBUG = bool( os.environ.get('DJANGO_DEBUG', True) )
ALLOWED_HOSTS = [
'shrouded-ocean-19461.herokuapp.com', 'localhost', '127.0.0.1',
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_celery_results',
'Buylist',
#'django_q',
]
MIDDLEWARE = [
'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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
PROJECT_DIR = os.path.dirname(__file__)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_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 = 'project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
# Heroku: Update database configuration from $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# The URL tos use when referring to static files (where they will be served from)
STATIC_URL = '/static/'
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
#CELERY_RESULT_BACKEND = 'django-db'
project/init.py
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']
Here is my procfile
I have the Heroku Redis app installed on Heroku's end
web: gunicorn project.wsgi --log-file -
worker: celery worker --app=tasks.app
Procfile is located in the main Git root directory. Let me know if you need more info and I can provide it! Thanks a bunch!
The procfile for a single worker should look something like this
worker: celery -A <folder containing celery.py> worker -l info
My broker url was also not configured correctly. Setup your broker on heroku, and click on it from the overview page once you have added it to your project. In my case I used CloudAMQP - RabbitMQ manager. Once you click on it, there will be information about your broker url. Password, username, url etc. You want to copy the url, and in your Django app's settings.py or somewhere where you are configuring your Celery config files, you want to set your broker url. Mine looks like this in settings.py
CELERY_BROKER_URL = 'amqp://<USER>:<PASSWORD>#chimpanzee.rmq.cloudamqp.com/<USER>'
And that's after putting the the following line in celery.py to tell it to look in settings.py for configs.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app.config_from_object('django.conf:settings', namespace='CELERY')
Push it to heroku, and see if that solves your problem. If you're trying to start the worker on windows locally, you have to use the eventlet option in your terminal.
Hope this helps, and let me know if you're still having problems.