Django + Heroku deployment - django

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())

Related

Django & Migrate Error: I can't migrate my project

I was trying to run a Django project which I got from someone else.
But I can't do the migration.
I came from a mobile app development background. So I'm quite new to this backend thing including Django.
Please have a look and help with this issue. Thank you
Traceback (most recent call last):
File "/Users/punreachrany/Desktop/MyProject/manage.py", line 22, in <module>
main()
File "/Users/punreachrany/Desktop/MyProject/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/core/management/__init__.py", line 420, in execute
django.setup()
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/site-packages/django/apps/config.py", line 228, in create
import_module(entry)
File "/Users/punreachrany/opt/anaconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'bootstrap4'
Bootstrap is a dependency you need to add. You can install it by running the below command before migrating.(This is a one time setup)
python -m pip install bootstrap4
Since you mentioned you got the project from someone else.In that case check if there is a file called requirements.txt.
If it exists, run the below command from that directory to install all the required dependencies.
Python -m pip install -r requirements.txt

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 INSTALLED_APP Resolving to "ModuleNotFoundError"

The pip module Im using needs to be included in installed apps as such:
INSTALLED_APPS = [
...,
'django_apscheduler',
...
]
But I keep getting the following when I build my app:
ModuleNotFoundError: No module named 'django_apscheduler'
Even though the app is clearly listed in my pip list. The module in question is Django APScheduler.
EDIT: Ive already installed the module via pip install django_apscheduler, but it is listed in pip list as:
django-apscheduler # using - as opposed to _
EDIT 2: Full trace:
+ python3 manage.py test --settings=grin_app.settings.test
Traceback (most recent call last):
File "manage.py", line 25, in <module>
execute_from_command_line(sys.argv)
File "/root/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/root/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/root/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/root/.local/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/root/.local/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/local/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 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'django_apscheduler'
this error happens when you have NOT installed the app.
run pip install django_apscheduler and try again.
make sure you are installing it in your python environment or in your appropriate virtual environment

How To Solve Error: ImproperlyConfigured: mysqlclient 1.3.13 or newer is required in Django on Windows?

I follow the tutorial from Traversy Media on Youtube videos. When I put the command
python manage.py migrate
Then I got such an error like this:
C:\Users\Acer\Project\djangoproject>python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\db\models\base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\db\models\base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\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 "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\db\__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\db\utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\db\utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\importlib\__
init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\Acer\AppData\Local\Programs\Python\Python37-32\lib\site-package
s\django\db\backends\mysql\base.py", line 36, in <module>
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you hav
e %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is requ
ired; you have 0.9.3.
Btw, I already install the C++ Build Visual Studio from another error that I got, and I also installed already the mysqlclient-1.4.4-cp37-cp37m-win32.whl to get it done. But still, it gives me such an error.
Please help me, and thank you for those who already respond on this.
Are you using a virtual environment? If you have no idea what I'm talking about, see this blog for a good explanation for what a virtual environment is.
For this example I will be using virtualenv but any virtual environment will do.
steps:
open CMD/Terminal and cd into your project folder
run virtualenv ./env && source env/bin/activate
(this create theenv` directory, and starts your virtual environment)
run pip3 install django mysqlclient
(this installs django and mysqlclient into your virtual environment)
Now your migrations should work as expected.
Remember that you have to run source env/bin/activate every time that you open a new CMD/Terminal window (inside of your root project folder)
libmariadb-dev-compat: /usr/bin/mysql_config Debian

Django startserver not recognizing built in python libraries?

I am running Python 3.7 and Django 2.1 I am using Django for the first time. I have set up a virtual environment to run my project. I have imported django into the virtual environment using pip, but when I try to issue the command
python manage.py runserver
I get the following error code
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000203257A1B70>
Traceback (most recent call last):
File "C:\Environments\django_env\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Environments\django_env\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Environments\django_env\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\Environments\django_env\lib\site-packages\django\core\management\__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "C:\Environments\django_env\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Environments\django_env\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Environments\django_env\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Environments\django_env\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Environments\django_env\lib\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 "C:\Environments\django_env\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Environments\django_env\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Environments\django_env\lib\site-packages\django\db\models\base.py", line 101, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Environments\django_env\lib\site-packages\django\db\models\base.py", line 304, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Environments\django_env\lib\site-packages\django\db\models\options.py", line 203, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Environments\django_env\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Environments\django_env\lib\site-packages\django\db\utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Environments\django_env\lib\site-packages\django\db\utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Environments\django_env\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Environments\django_env\lib\site-packages\django\db\backends\sqlite3\base.py", line 10, in <module>
from sqlite3 import dbapi2 as Database
File "c:\users\aguec\anaconda3\Lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "c:\users\aguec\anaconda3\Lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed: The specified module could not be found.
I have spent many hours searching for the problem online already but can't find a solution to my problem. I have opened python to check if sqlite3 is installed and it is. I have tried to use pip install sqlite3 in the virtual environment but that doesn't work.
I thought that when you create a virtual environment, Python already includes the libraries sqlite3 and _sqlite3?
Please any help would be appreciated.
You have multiple versions of python installed - virtualenv and anaconda which is causing conflicts. You will have to activate environ before running or directly use anaconda based environ as it has sqlite installed. Install sqlite package in your own virtualenv if that is what you intend to you using pip install.
Although not related to your package but below link will be useful in resolving the issue
https://github.com/pytorch/pytorch/issues/9263