heroku deployment, relation "django_session" does not exist - python

I've build a simple browser game on Python+Django, which is using session/cookies to track the score. The game it self doesn't have any database. When I'm deploying to heroku, I got this type of error
ProgrammingError at /
relation "django_session" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_session" WHERE "django_sessio...
^
Request Method: GET
Request URL: https://lesgogo.herokuapp.com/
Django Version: 3.0.7
Exception Type: ProgrammingError
Exception Value:
relation "django_session" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_session" WHERE "django_sessio...
^
Exception Location: /app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py in _execute, line 86
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.8.3
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python38.zip',
'/app/.heroku/python/lib/python3.8',
'/app/.heroku/python/lib/python3.8/lib-dynload',
'/app/.heroku/python/lib/python3.8/site-packages']
Server time: Sat, 13 Jun 2020 04:00:23 +0000
what I've tried:
deployed successfully to heroku without views.py (the logic part with session in that file), static files like img and css were loaded/showed properly
I was here and tried that:
python manage.py migrate --fake sessions zero
# then your sessions migrate will be
python manage.py showmigrations
sessions
[ ] 0001_initial
# then migrate with --fake-initial again
python manage.py migrate --fake-initial
added, and pushed back to heroku, didn't worked.
my settings.py file is pretty much default, so all middlewares are there, I just added my app name
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',
'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',
]
the versions i'm using
python-3.8.3
asgiref==3.2.7
dj-database-url==0.5.0
Django==3.0.7
django-heroku==0.3.1
gunicorn==20.0.4
psycopg2==2.8.5
pytz==2020.1
sqlparse==0.3.1
whitenoise==5.1.0
PS: it's my first post. Thanks!

After following the advice in this link ProgrammingError: relation "django_session" does not exist (it is the link you mention above), I then did
heroku run python manage.py makemigrations sessions
heroku run python manage.py migrate sessions

Related

ASGI_APPLICATION not working with Django Channels

I followed the tutorial in the channels documentation but when I start the server python3 manage.py runserver it gives me this :
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
October 17, 2022 - 00:13:21
Django version 4.1.2, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
when I expected for it to give me this :
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
October 17, 2022 - 00:13:21
Django version 4.1.2, using settings 'config.settings'
Starting ASGI/Channels version 3.0.5 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
settings.py
INSTALLED_APPS = [
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
]
ASGI_APPLICATION = 'config.asgi.application'
asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = ProtocolTypeRouter({
'http': get_asgi_application(),
})
It doesn't give any errors even when I change the ASGI_APPLICATION = 'config.asgi.application to ASGI_APPLICATION = ''.
This could be due to the fact that the Django and channels versions you have used are not compatible
Try : channels==3.0.4 and django==4.0.0
Had the same with django=4.1.4, channels=4.0.0
My solution:
install channels with daphne (i did not remove or did anything to previously installed channels; operating in the same venv)
python -m pip install -U channels["daphne"]
put daphne in INSTALLED APPS, do not put channels there:
INSTALLED_APPS = ( "daphne", "django.contrib.auth",...
in settings.py add ASGI_APPLICATION:
ASGI_APPLICATION = "myproject.asgi.application"
Use version of python that support by channels, you will found it at pypi channels page
I had the same problem, and found that there was a new release of Channels. Since the project's Pipfile did not specify a version, it was automatically upgraded.
Maybe you had the same issue, your question was asked 2 days after Channels v4.0 release.
Downgrading to v3.0.5 again solved the problem until I can properly upgrade.
At the first:
pip install daphne
pip install channels
And then update setting.py:
INSTALLED_APPS = [
'daphne',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Just this.

Operational error while adding values in a text field in django

I get this error when I add a something to the text field.I'm trying to create a project where someone can enter random messages.
OperationalError at /admin/submit/submit/add/
no such table: main.auth_user__old
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/submit/submit/add/
Django Version: 2.1
Exception Type: OperationalError
Exception Value:
no such table: main.auth_user__old
Exception Location: D:\Anaconda1\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 296
Python Executable: D:\Anaconda1\python.exe
Python Version: 3.8.3
Python Path:
['D:\\PyProjects\\Website',
'D:\\Anaconda1\\python38.zip',
'D:\\Anaconda1\\DLLs',
'D:\\Anaconda1\\lib',
'D:\\Anaconda1',
'C:\\Users\\heman\\AppData\\Roaming\\Python\\Python38\\site-packages',
'D:\\Anaconda1\\lib\\site-packages',
'D:\\Anaconda1\\lib\\site-packages\\win32',
'D:\\Anaconda1\\lib\\site-packages\\win32\\lib',
'D:\\Anaconda1\\lib\\site-packages\\Pythonwin']
Server time: Mon, 5 Oct 2020 17:19:26 +000
This is my models.py
from django.db import models
# Create your models here.
class Submit(models.Model):
title = models.TextField(max_length=140, default='SOME STRING')
status = models.TextField(max_length=140, default='SOME STRING')
description = models.TextField(max_length=140, default='SOME STRING')
Here is my setting.py installed apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'submit.apps.SubmitConfig',
]
Here is my apps.py
from django.apps import AppConfig
class SubmitConfig(AppConfig):
name = 'submit'
Here is my admin.py
from django.contrib import admin
from .models import Submit
# Register your models here.
admin.site.register(Submit)
I also ran python manage.py makemigrations
and python manage.py migrate
Please help me...Thanks in advance
This seems to work for me..
the original answer:
https://stackoverflow.com/a/59349457/13732795
But I'll copy paste it here too
Go to the virtual environment and install django#2.1.7
pip install django==2.1.7
Delete the db.sqlite3 file in your root folder.
Create the new db.sqlite3 in your root folder.
Re-run migrations:
python manage.py makemigrations
python manage.py migrate

Django migration didn't migrate authtoken and sessions

When running python manage.py migrate not all migrations were run, specifically django_celery_results, authtoken and sessions. This resulted in the application related migrations erroring out.
However, if I first manually migrate those three, and then specifically migrate auth (not sure why I'd need to migrate that again) and then do python manage.py migrate it'll work.
The installed apps on Django are like so:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'django_celery_results',
'celery.contrib.testing.tasks',
'api_app'
]
I'm wondering why that's happening, I thought migrate will run all the migrations listed in "operations to perform".
Your api_app.0002 migration creates a user without setting last_login. Therefore this migration must be run after the auth 0005 migration that allows nulls in this column.
If you add a dependency to your migration, then Django will run them in the correct order.
class Migration(migrations.Migration):
dependencies = [('auth', '0005_alter_user_last_login_null')]

Apply migrations and models from all the apps

I'm using Django and I have an schema like
mainapp
|---mainapp
| |---migrations.py
| |---models/
|---app2
|---migrations/
|---models/
But, when I execute:
python manage.py migrate it is generationg the tables of mainapp/models, but no the app2/models and app2/migrations either.
How can execute those migrations?
first of all try
python manage.py makemigrations
for a specific app
python manage.py makemigrations appname
this will migrate all the apps
then
python manage.py migrate
hope it helps
Make sure you have added the app in the installed apps.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainapp',
'app2',
#......,
#......,
]
Then create migrations
using python mananage.py makemigrations and migrate with python manange.py migrate

AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL

Here is my settings.
########## APP CONFIGURATION
DJANGO_APPS = (
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Useful template tags:
# 'django.contrib.humanize',
# Admin panel and documentation:
'django.contrib.admin',
# 'django.contrib.admindocs',
)
THIRD_PARTY_APPS = (
'south',
'colorful',
#'grappelli',
'django_extensions',
#'rest_framework',
#'sorl.thumbnail',
'guardian',
'sslserver',
'djangosecure',
'django_nose',
)
# Apps specific for this project go here.
LOCAL_APPS = (
'main',
'executor',
'workshop',
'management',
)
And here is my loaded python apps:
Django < 1.7
South === 1.0
django-colorful
django-extensions == 1.5.2
django-mptt == 0.7.4
loremipsum < 1.0.4
django_debug_toolbar
werkzeug == 0.11.4
Pillow == 3.1.1
MySQL-python
xlwt
xlrd
django-guardian == 1.3.2
lxml
fabric
django-fab-deploy
django-secure === 1.0.1
django-sslserver === 0.15
python-ldap === 2.4.19
django_auth_ldap
html2text
htmlentities
openpyxl
raven
colorful
ipython == 4.1.1
django-nose
newrelic
I have done none code changes and now I can't create or update instances. I will get this error message in server:
File "/srv/www/lohja/targetor/env/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 129, in get_user_model
[targetor_upgrade#targetorpro.fi] out:
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
[targetor_upgrade#targetorpro.fi] out:
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'executor.ExecutorUser' that has not been installed
[targetor_upgrade#targetorpro.fi] out:
Fatal error: run() received nonzero return code 1 while executing!
Requested: /srv/www/lohja/targetor/env/bin/python /srv/www/lohja/targetor/manage.py migrate --fake --no-initial-data guardian --settings=settings.local_setti$
Executed: /bin/bash -l -c "cd /srv/www/lohja/targetor/env/bin/ && . /srv/www/lohja/targetor/env/bin/activate && /srv/www/lohja/targetor/env/bin/python /srv/w$
Aborting.
I guess there are some changes in python apps which will do this error. But I can't find which version of certain apps I have to use. I had have already this kind of problem but managed to solve those. Now, I can't find the reason... Does anyone else fix this issue?
Could be due to missing django make sure all the requirement have been install. If you are using VirtualEnv make sure you activate prior to starting your server using manage.py

Categories