django deployment on Heroku KeyError: 'MAILGUN_API_KEY' - django

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

Related

How to access Heroku config variables inside Django settings

I am trying to push my code on Heroku, I have hidden my secret key using environ package but now Heroku is not able to access it since I have ignored my .env file using gitignore, I have read about config vars in Heroku but I am having trouble understanding how do I make Django access those values
import os
import environ
# from .secret import key
env = environ.Env()
environ.Env.read_env()
SECRET_KEY = env('KEY',default=env('SECRET_KEY'))
updated code
In the log, it is giving me error like this
warnings.warn(
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 277, in get_value
value = self.ENVIRON[var]
File "/app/.heroku/python/lib/python3.8/os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 244, in fetch_command
settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/build_067d1d45/food_deliveryapp/settings.py", line 22, in <module>
SECRET_KEY = os.environ.get('KEY',env('SECRET_KEY'))
File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 127, in __call__
return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 281, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
For every variable in .env create a Config Var, make sure names are UPPERCASE.
In your code you can access them via environ (map with all env variables):
my_secret = (os.environ.get("MY_SECRET", 'dev default value')

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.

ModuleNotFoundError psycopg2 when deploying in heroku even though psycopg2 is installed

I migrated my website from sqlite3 to postgresql. I'm deploying my website in heroku but this error came up when I'm executing command
push heroku master
-----> Python app detected
! Python has released a security update! Please consider upgrading to python-3.7.3
Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Installing python-3.7.2
-----> Installing pip
-----> Installing dependencies with Pipenv 2018.5.18…
Installing dependencies from Pipfile…
-----> Installing SQLite3
-----> $ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/app/.heroku/python/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/build_bda0f9a0e468e1cd92eba305080085ba/home/models.py", line 3, in <module>
from wagtail.core.models import Page
File "/app/.heroku/python/lib/python3.7/site-packages/wagtail/core/models.py", line 8, in <module>
from django.contrib.auth.models import Group, Permission
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
I don't know why it says import error since I already installed psycopg2 in my virtual environment. The python shell does not even throw some errors when I import it and I can run my server successfully in my local machine
In my requirements.txt file, psycopg2==2.8.3 is listed there.
My Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
django = "<2.3,>=2.2"
wagtail = "<2.6,>=2.5"
[requires]
python_version = "3.7"
My database configuration in base.py of settings folder
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'personalsite',
}
}
in production.py of my settings folder
DATABASES['default'] = dj_database_url.config()
in requirements.txt
psycopg2==2.8.2
beautifulsoup4==4.6.0
certifi==2019.6.16
chardet==3.0.4
dj-database-url==0.5.0
dj-static==0.0.6
Django==2.2.2
django-modelcluster==4.4
django-taggit==0.24.0
django-toolbelt==0.0.1
django-treebeard==4.3
djangorestframework==3.9.4
draftjs-exporter==2.1.6
gunicorn==19.9.0
html5lib==1.0.1
idna==2.8
Pillow==5.4.1
pytz==2019.1
requests==2.22.0
six==1.12.0
sqlparse==0.3.0
static3==0.7.0
Unidecode==1.1.0
urllib3==1.25.3
wagtail==2.5.1
webencodings==0.5.1
Willow==1.1
It seems like you installed all those packages to your local Python installation and not to your virtual environment. Your Pipfile doesn't reflect the packages such as psycopg2, dj_database_url etc. Please install the packages to your virtual environment.
First, activate your virtual environment,
pipenv shell
Then install the following packages,
pipenv install psycopg2-binary
pipenv install dj-database-url
Run pipenv lock to generate the appropriate Pipfile.lock. And try to push it to Heroku once again using the following command.
push heroku master

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

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

Django + Heroku deployment

I tried several times now to deploy my Heroku application but I always receive the attached error message. After adding DISABLE_COLLECTSTATIC=1 it works and I can remove it after. But it never works when it's enabled which it should be. Here a picture of my folder structure if it's needed https://drive.google.com/file/d/11L8jpFzYfDYT3Ob4G-soaGhL0pgU24iZ/view?usp=sharing
! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
Detected buildpacks: Python,Node.js
See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Python app detected
-----> Installing python-3.6.6
-----> Installing pip
-----> Installing dependencies with Pipenv 2018.5.18…
Installing dependencies from Pipfile.lock (d438cb)…
-----> Installing SQLite3
-----> $ python manage.py collectstatic --noinput
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 114, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 315, in add_to_class
value.contribute_to_class(cls, name)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/options.py", line 205, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 199, in __getitem__
self.ensure_defaults(alias)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 171, in ensure_defaults
conn.setdefault('ATOMIC_REQUESTS', False)
AttributeError: 'NoneType' object has no attribute 'setdefault'
! Error while running '$ python manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
It might be unrelated to collectstatic. Similar exception was described here: python error AttributeError: 'str' object has no attribute 'setdefault'.
I'd suggest checking your DATABASES setting in your settings.py. Check out django-heroku (https://github.com/heroku/django-heroku) if you're not using it already and just use this in your settings:
import django_heroku
django_heroku.settings(locals())