Heroku - Set Procfile to run a Django Command to create DB - django

I'm deploying my DjangoApp to Heroku.
I'm following all the documentation but not sure how to set the Procfile to run my command, as I do locally to create my shop_peru DB:
This is what I do locally to create the Peru DB:
python manage.py ubigeo_peru
This is my Procfile:
release: python manage.py makemigrations
release: python manage.py migrate
release: python manage.py ubigeo_peru
web: gunicorn stickers_gallito.wsgi --log-file -
ubigeo_peru.py:
import pandas as pd
import csv
from shop.models import Peru
from django.core.management.base import BaseCommand
tmp_data=pd.read_csv('static/data/ubigeo-peru-2018-12-25.csv',sep=',', encoding="utf-8")
class Command(BaseCommand):
def handle(self, **options):
products = [
Peru(
departamento=row['departamento'],
provincia=row['provincia'],
distrito=row['distrito'],
)
for idx, row in tmp_data.iterrows()
]
Peru.objects.bulk_create(products)
When deplying to heroku I get this error:
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 795 bytes | 795.00 KiB/s, done.
Total 8 (delta 6), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: ! Python has released a security update! Please consider upgrading to python-3.6.8
remote: Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing requirements with pip
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote: Traceback (most recent call last):
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 204, in fetch_command
remote: app_name = commands[subcommand]
remote: KeyError: 'collectstatic'
remote: During handling of the above exception, another exception occurred:
remote: Traceback (most recent call last):
remote: File "manage.py", line 15, in <module>
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
remote: self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 211, in fetch_command
remote: settings.INSTALLED_APPS
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
remote: self._setup(name)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
remote: self._wrapped = Settings(settings_module)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 107, in __init__
remote: mod = importlib.import_module(self.SETTINGS_MODULE)
remote: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote: return _bootstrap._gcd_import(name[level:], package, level)
remote: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
remote: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
remote: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
remote: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote: File "/tmp/build_1d164976f07d1b5308e0df603fc5a5c0/stickers_gallito/settings.py", line 2, in <module>
remote: import django_heroku
remote: ModuleNotFoundError: No module named 'django_heroku'
remote:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
remote:
remote: You may need to update application code to resolve this error.
remote: Or, you can disable collectstatic for this application:
remote:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote: https://devcenter.heroku.com/articles/django-assets
remote: ! Push rejected, failed to compile Python app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to stickers-gallito-app.
remote:
To https://git.heroku.com/stickers-gallito-app.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/stickers-gallito-app.git'
(stickers_gallito_env) ogonzales#ogonzales:~/Desktop/web_proyects/stickers_gallito$ git push heroku master
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (12/12), 1.07 KiB | 1.07 MiB/s, done.
Total 12 (delta 9), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: ! Python has released a security update! Please consider upgrading to python-3.6.8
remote: Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing requirements with pip
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote: 167 static files copied to '/tmp/build_48b84f7db1a8a5df5eb464b11621fa61/staticfiles', 67 unmodified, 278 post-processed.
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> release, web
remote:
remote: -----> Compressing...
remote: Done: 115.8M
remote: -----> Launching...
remote: ! Release command declared: this new release will not be available until the command succeeds.
remote: Released v17
remote: https://stickers-gallito-app.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
remote: Running release command...
remote:
remote: Traceback (most recent call last):
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
remote: return self.cursor.execute(sql, params)
remote: psycopg2.ProgrammingError: relation "shop_peru" does not exist
remote: LINE 1: INSERT INTO "shop_peru" ("departamento", "provincia", "distr...
remote: ^
remote:
remote:
remote: The above exception was the direct cause of the following exception:
remote:
remote: Traceback (most recent call last):
remote: File "manage.py", line 15, in <module>
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
remote: self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
remote: self.execute(*args, **cmd_options)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
remote: output = self.handle(*args, **options)
remote: File "/app/shop/management/commands/ubigeo_peru.py", line 22, in handle
remote: Peru.objects.bulk_create(products)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
remote: return getattr(self.get_queryset(), name)(*args, **kwargs)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 465, in bulk_create
remote: ids = self._batched_insert(objs_without_pk, fields, batch_size)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1149, in _batched_insert
remote: inserted_id = self._insert(item, fields=fields, using=self.db, return_id=True)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1136, in _insert
remote: return query.get_compiler(using=using).execute_sql(return_id)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1289, in execute_sql
remote: cursor.execute(sql, params)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
remote: return super().execute(sql, params)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
remote: return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
remote: return executor(sql, params, many, context)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
remote: return self.cursor.execute(sql, params)
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
remote: raise dj_exc_value.with_traceback(traceback) from exc_value
remote: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
remote: return self.cursor.execute(sql, params)
remote: django.db.utils.ProgrammingError: relation "shop_peru" does not exist
remote: LINE 1: INSERT INTO "shop_peru" ("departamento", "provincia", "distr...
remote: ^
remote:
remote: Waiting for release.... failed.
To https://git.heroku.com/stickers-gallito-app.git
ab60b75..16f4a2f master -> master
Structure:
stickers_gallito (project)
|_shop (app)
|_management
|_commands
|_ubigeo_peru.py
Settings:
WSGI_APPLICATION = 'stickers_gallito.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ddl7v8vi4kdkip',
'USER': 'u6sic8t2nrdejq',
'PASSWORD': 'p72e949360dda1614aabf9a51b531d0c4a5f06ecc7e6528e8454f81687b7a321d',
'HOST': 'ec2-54-157-94-8.compute-1.amazonaws.com',
'PORT': '5432',
}
}

You cannot use sqlite database in Heroku. Use on of the databases they provide.
I generally use postgresql
heroku addons:create heroku-postgresql:hobby-dev
This will create an empty database for you
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# add this
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
This is will change the database in your setting.py

Related

Django CustomUser deploying and getting error: relation "accounts_customuser" does not exist

I am deploying my django to VSP using Dokku. I have the project working on my local computer but when I deploy to dokku, I'm getting errors when it tries to do the migrations. The dokku is deployed with git push dokku main:master and migrations are in my .gitignore, so migrations on my local computer are not being pushed. I am using a CustomUser model which extends AbstractUser.
The problem looks to be django.db.utils.ProgrammingError: relation "accounts_customuser" does not exist.
full logs/trace
PS C:\> git push dokku main:master
Enter passphrase for key '/c/Users/name/.ssh/id_rsa':
Enumerating objects: 331, done.
Counting objects: 100% (331/331), done.
Delta compression using up to 16 threads
Compressing objects: 100% (326/326), done.
Writing objects: 100% (331/331), 1.18 MiB | 2.24 MiB/s, done.
Total 331 (delta 71), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (71/71), done.
remote: -----> Cleaning up...
remote: -----> Building app from herokuish
remote: -----> Adding BUILD_ENV to build environment...
remote: -----> Python app detected
remote: -----> Using Python version specified in Pipfile.lock
remote: cp: cannot stat '/tmp/build/requirements.txt': No such file or directory
remote: -----> Installing python-3.8.11
remote: -----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
remote: -----> Installing dependencies with Pipenv 2020.11.15
remote: Installing dependencies from Pipfile.lock (ce16c9)...
remote: -----> Installing SQLite3
remote: -----> $ python manage.py collectstatic --noinput
remote: 130 static files copied to '/tmp/build/staticfiles', 12 unmodified.
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> release, web
remote: -----> Releasing app...
remote: -----> Checking for predeploy task
remote: No predeploy task found, skipping
remote: -----> Checking for release task
remote: -----> Executing release task from Procfile: python manage.py migrate --noinput
remote: =====> Start of app release task (f02af62b3) output
remote: success
remote: Operations to perform:
remote: Apply all migrations: account, admin, auth, contenttypes, sessions, sites
remote: Running migrations:
remote: Traceback (most recent call last):
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
remote: return self.cursor.execute(sql, params)
remote: psycopg2.errors.UndefinedTable: relation "accounts_customuser" does not exist
remote: The above exception was the direct cause of the following exception:
remote: Traceback (most recent call last):
remote: File "manage.py", line 22, in <module>
remote: main()
remote: File "manage.py", line 18, in main
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
remote: self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
remote: self.execute(*args, **cmd_options)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
remote: output = self.handle(*args, **options)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
remote: res = handle_func(*args, **kwargs)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 244, in handle
remote: post_migrate_state = executor.migrate(
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
remote: state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
remote: state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 230, in apply_migration
remote: migration_recorded = True
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 118, in __exit__
remote: self.execute(sql)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 145, in execute
remote: cursor.execute(sql, params)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
remote: return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
remote: return executor(sql, params, many, context)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
remote: return self.cursor.execute(sql, params)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
remote: raise dj_exc_value.with_traceback(traceback) from exc_value
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
remote: return self.cursor.execute(sql, params)
remote: django.db.utils.ProgrammingError: relation "accounts_customuser" does not exist
remote: Applying account.0001_initial...
remote: =====> End of app release task (f02af62b3) output
remote: ! Execution of release task failed: python manage.py migrate --noinput
remote: 2021/08/20 08:28:22 exit status 1
remote: ! exit status 1
remote: 2021/08/20 08:28:22 exit status 1
remote: 2021/08/20 08:28:22 exit status 1
remote: 2021/08/20 08:28:22 exit status 1
To myIP:app
! [remote rejected] main -> master (pre-receive hook declined)
settings.py
AUTH_USER_MODEL = 'accounts.CustomUser'
accounts/models.py
import uuid
from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
is_teacher = models.BooleanField('teacher status', default=False)
def __str__(self):
return self.username
gradebook/models.py for an example of how I use the CustomUser / AUTH_USER_MODEL
class Course(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
course_name = models.CharField(max_length=20)
I have thought about trying to run python manage.py migrate --fake but I don't know what the consequences of that might be.

django deployment on Heroku KeyError: 'MAILGUN_API_KEY'

I m using cookie cutter but did not select Heroku deployment in selection list when creating app.
but now, I want it on Heroku.
I've this error:
File "/app/.heroku/python/lib/python3.8/os.py", line 675, in __getitem__
remote: raise KeyError(key) from None
remote: KeyError: 'MAILGUN_API_KEY'
remote: During handling of the above exception, another exception occurred:
Traceback (most recent call last):
remote: File "manage.py", line 31, in <module>
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
remote: self.fetch_command(subcommand).run_from_argv(self.argv)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 231, in fetch_command
remote: settings.INSTALLED_APPS
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
remote: self._setup(name)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
remote: self._wrapped = Settings(settings_module)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
remote: mod = importlib.import_module(self.SETTINGS_MODULE)
remote: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
remote: return _bootstrap._gcd_import(name[level:], package, level)
remote: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
remote: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
remote: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
remote: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
remote: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
remote: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote: File "/tmp/build_3a322b24/config/settings/production.py", line 132, in <module>
remote: "MAILGUN_API_KEY": env("MAILGUN_API_KEY"),
remote: File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 123, in __call__
remote: return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
remote: File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 277, in get_value
remote: raise ImproperlyConfigured(error_msg)
remote: django.core.exceptions.ImproperlyConfigured: Set the MAILGUN_API_KEY environment variable
remote:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
packages installed are:
remote: Installing collected packages: pytz, text-unidecode, python-slugify, Pillow, pycparser, cffi, six, argon2-cffi, redis, hiredis, sqlparse, asgiref, django, django-environ, django-model-utils, defusedxml, python3-openid, oauthlib, urllib3, chardet, idna, certifi, requests, requests-oauthlib, cryptography, pyjwt, django-allauth, django-crispy-forms, django-redis, bootstrap4, whitenoise, gunicorn, psycopg2, typing-extensions, python-dateutil, jmespath, botocore, s3transfer, boto3, django-storages, Collectfast, django-anymail
remote: Successfully installed Collectfast-2.2.0 Pillow-8.2.0 argon2-cffi-20.1.0 asgiref-3.4.0 bootstrap4-0.1.0 boto3-1.17.101 botocore-1.20.101 certifi-2021.5.30 cffi-1.14.5 chardet-4.0.0 cryptography-3.4.7 defusedxml-0.7.1 django-3.1.12 django-allauth-0.44.0 django-anymail-8.2 django-crispy-forms-1.11.2 django-environ-0.4.5 django-model-utils-4.1.1 django-redis-4.12.1 django-storages-1.11.1 gunicorn-20.1.0 hiredis-1.1.0 idna-2.10 jmespath-0.10.0 oauthlib-3.1.1 psycopg2-2.9.1 pycparser-2.20 pyjwt-2.1.0 python-dateutil-2.8.1 python-slugify-5.0.2 python3-openid-3.2.0 pytz-2021.1 redis-3.5.3 requests-2.25.1 requests-oauthlib-1.3.0 s3transfer-0.4.2 six-1.16.0 sqlparse-0.4.1 text-unidecode-1.3 typing-extensions-3.10.0.0 urllib3-1.26.6 whitenoise-5.2.0
remote: -----> $ python manage.py collectstatic --noinput
what should I add to make it works

Django-Heroku-PostGres: KeyError: 'psql' during Git push Heroku main

I'm trying to deploy my Django app in Heroku. I'm also using PostGreSQL and Github. I've been using a bunch of random tutorials to set this up and try to debug the results, so I apologize if there are "inconsistencies".
When I run git push heroku main, I get the error remote: KeyError: 'psql' Error while running '$ python manage.py collectstatic --noinput:
Here's the traceback.
remote: -----> $ python manage.py collectstatic --noinput
remote: Traceback (most recent call last):
remote: File "/tmp/build_e87847fa/manage.py", line 22, in <module>
remote: main()
remote: File "/tmp/build_e87847fa/manage.py", line 18, in main
remote: execute_from_command_line(sys.argv)
remote: File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
remote: utility.execute()
remote: File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
remote: settings.INSTALLED_APPS
remote: File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
remote: self._setup(name)
remote: File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
remote: self._wrapped = Settings(settings_module)
remote: File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
remote: mod = importlib.import_module(self.SETTINGS_MODULE)
remote: File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote: return _bootstrap._gcd_import(name[level:], package, level)
remote: File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote: File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote: File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote: File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote: File "<frozen importlib._bootstrap_external>", line 790, in exec_module
remote: File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote: File "/tmp/build_e87847fa/medrec_project/settings.py", line 106, in <module>
remote: DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
remote: File "/app/.heroku/python/lib/python3.9/site-packages/dj_database_url.py", line 55, in config
remote: config = parse(s, engine, conn_max_age, ssl_require)
remote: File "/app/.heroku/python/lib/python3.9/site-packages/dj_database_url.py", line 103, in parse
remote: engine = SCHEMES[url.scheme] if engine is None else engine
remote: KeyError: 'psql'
remote:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
This is what I think is relevant from my settings file:
import os
import django_heroku
import dj_database_url
import psycopg2
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD':os.getenv('DB_PASS'),
'HOST': 'localhost',
'PORT':'5432',
}
}
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
django_heroku.settings(locals())
The only place in my code that has psql is in my .env file:
SECRET_KEY='<mysecretkey>'
DATABASE_URL='psql://urser:un-githubbedpassword#127.0.0.1:8458/database'
SQLITE_URL='sqlite:///my-local-sqlite.db'
CACHE_URL='memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213'
REDIS_URL='rediscache://127.0.0.1:6379/1?client_class=django_redis.client.DefaultClient&password=ungithubbed-secret'
DB_NAME='medrecapp'
DB_USER='<myusername>'
DB_PASS='<mypassword>'
DB_HOST='127.0.0.1'
When I run py manage.py collectstatic everything runs fine.
As far as I can tell, I've added PostGreSQL to PATH (points to the bin folder of PostGreSql).
I can use psql in my terminal without any problems
I can push changes to git without issues.
I've updated my Heroku Settings Config Vars with everything from my .env file.
Does anyone know how to solve this?
Thanks!
The solution is to change the address for postgres in the .env from 'psql://...' to 'pgsql://...'
in your case will be something like
DATABASE_URL='pgsql://urser:un-githubbedpassword#127.0.0.1:8458/database'
Knowing why it is happening is not necesary for this to work, but if you are interested... This is happening because dj_database_url at some point select the engine for the database based on the first word of DATABASE_URL.
Checking the SCHEMAS array we can see
{'postgres': 'django.db.backends.postgresql_psycopg2', 'postgresql': 'django.db.backends.postgresql_psycopg2', 'pgsql': 'django.db.backends.postgresql_psycopg2', 'postgis': 'django.contrib.gis.db.backends.postgis', 'mysql': 'django.db.backends.mysql', 'mysql2': 'django.db.backends.mysql', 'mysqlgis': 'django.contrib.gis.db.backends.mysql', 'mysql-connector': 'mysql.connector.django', 'mssql': 'sql_server.pyodbc', 'spatialite': 'django.contrib.gis.db.backends.spatialite', 'sqlite': 'django.db.backends.sqlite3', 'oracle': 'django.db.backends.oracle', 'oraclegis': 'django.contrib.gis.db.backends.oracle', 'redshift': 'django_redshift_backend'}
they had entries for 'postgres', 'pgsql' and 'postgresql' but no for 'psql', that's why we got the 'KeyError' error.

Heroku push rejected (Django app)

I'm deploying a Django app to GitHub and Heroku.
I have deployed it first to GitHub, removing (putting it in the .gitignore file) "settings.py", and now that I'm trying to push it to Heroku, I get an error:
ImportError: No module named settings
remote:
remote: ! Error while running '$ python manage.py collectstatic --noinput'.
remote: See traceback above for details.
remote:
remote: You may need to update application code to resolve this error.
remote: Or, you can disable collectstatic for this application:
remote:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
remote:
remote: https://devcenter.heroku.com/articles/django-assets
remote:
remote: ! Push rejected, failed to compile Python app
How can I avoid to push some file to GitHub, but at the same having the possibility to pushing it to Heroku?
UPDATE:
I've tried to keep settings.py and temp_settings.py separate, but now the app doesn't work anymore. That's the traceback when I try to run the local server:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 303, in execute
settings.INSTALLED_APPS
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/stefano/projects/blog-project/blogprojectenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/stefano/projects/blog-project/blogproject/settings.py", line 104, in <module>
DATABASES['default'].update(db_from_env)
NameError: name 'DATABASES' is not defined
It seems settings.py can't "reach" temp_settings.py, as I've cut-paste all of the database settings in temp_settings. Is that wrong?
The common practice is not to ignore settings.py file from your code base. Instead, do something like:
try:
from temp_settings import *
except ImportError:
pass
at the end of settings.py file and add temp_settings.py to .gitignore
For the credentials that you need to keep private, use temp_settings.

KeyError: 'AWS_STORAGE_BUCKET_NAME' When trying to deploy to Heroku

I am trying to deploy my Django website onto Heroku and Amazon S3. However, after I typed git push heroku master, I got this:
Counting objects: 4, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 408 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing dependencies with pip
remote:
remote: -----> Preparing static assets
remote: Collectstatic configuration error. To debug, run:
remote: $ heroku run python ./manage.py collectstatic --noinput
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing... done, 52.0MB
remote: -----> Launching... done, v10
remote: https://article-django.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/article-django.git
070a1af..c4bbac5 master -> master
Then I did heroku run python ./manage.py collectstatic --noinput and got this traceback:
Running `python ./manage.py collectstatic --noinput` attached to terminal... up, run.8499
/app/static/
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django /core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 94, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/app/django_test/settings.py", line 142, in <module>
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
File "/app/.heroku/python/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'AWS_STORAGE_BUCKET_NAME'
I also have my Procfile:
web: gunicorn django_test.wsgi
And this snippet of my settings.py file:
try:
from local_settings import *
except Exception as e:
print e.message
if not DEBUG:
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
S3_URL = 'http://%s.s3.amazonaws.com/assets/' % article-deanna
STATIC_URL = S3_URL
I committed all of the changes but I don't know why I still run into this error.
You need to set the AWS_STORAGE_BUCKET_NAME environment variable on Heroku.
Try the following (inserting the name of your S3 bucket):
heroku config:set AWS_STORAGE_BUCKET_NAME=<YOUR BUCKET NAME>