(Django version 1.9, Python version 3.4.3, using virtual env made with mkvirtualenv --python=/usr/bin/python3.4 django19)
I've been following the Django tutorial very closely, without any problems up til part 5:
https://docs.djangoproject.com/en/1.9/intro/tutorial05/
although, strictly speaking, the problem isn't to do with part 5, but bear with me. I'm following the tutorial in the pythonanywhere framework, because eventually I would like to host a website there. Therefore, I am also following the 'Following the Django tutorial' offered by pythonanywhere:
https://help.pythonanywhere.com/pages/FollowingTheDjangoTutorial
although my problem isn't anticipated in the advice for part 5 in the tutorial. Now the problem- at the point in the Django tutorial which reads:
To check if the bug really exists, using the Admin create a question whose date lies in the future and check the method using the shell
Following the link to the shell, I try (as instructed in the link) to run:
django-admin shell --plain
However I get the following error message
(django19)19:11 ~/mysite $ django-admin shell --plain
Traceback (most recent call last):
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 391, in execute saved_locale = translation.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 176, in get_language
return _trans.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 57, in __getattr__
if settings.USE_I18N:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
During handling of the above exception, another exception occurred:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 360, in run_from_argv
connections.close_all()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 230, in close_all
for alias in self:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 224, in __iter__
return iter(self.databases)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 157, in databases
self._databases = settings.DATABASES
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This error messages recommends setting DATABASES (which is set in mysite/settings.py), by defining the variable DJANGO_SETTINGS_MODULE. So, that's what I did:
export DJANGO_SETTINGS_MODULE="${PWD}/mysite/settings.py"
There are also the required __init__.py files in the mysite and mysite/mysite directories. However now when I run the django-admin command I get:
ImportError: No module named 'mysite/settings'
I can't find anymore documentation or help hereafter. What am I missing? Do I need to set something special somewhere else? Am I setting DJANGO_SETTINGS_MODULE correctly? The ImportError makes it sound like I should set DJANGO_SETTINGS_MODULE to mysite.settings or something similar, but this doesn't work either.
Once you have created your project, use manage.py instead of django-admin to run commands:
python manage.py shell
The manage.py command takes care of the settings for you. If you use django-admin, you have to set DJANGO_SETTINGS_MODULE manually. The DJANGO_SETTINGS_MODULE should be the Python module, e.g. mysite.settings, not a file like /path/to/mysite/settings.py.
Be sure to have your project in the python import search path. Try this
>>import sys
>>sys.path
if the path/directory for your project is not listed, then you need to do
>>sys.path.append('/path/to/my/django/project')
Then assign DJANGO_SETTINGS_MODULE like so:
$export DJANGO_SETTINGS_MODULE=mysite.settings
See Designating Django settings
Related
I keep getting an Improperly Configured Error when I try to run my server
Error message:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I created a new django project,
I have django installed in my virtual environment,
I did not forget to activate the virtual environment,
And I triple checked my python path,
Here is the full traceback:
(venv3) C:\Users\user\Desktop\trialproject>django-admin.py runserver
Traceback (most recent call last):
File "C:\Users\user\Desktop\trialproject\venv3\Scripts\django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
super().execute(*args, **options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\conf\__init__.py", line 57, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Someone please help me figure out the problem. Thanks
that usually happen when you want to run your server with wrong virtual environment.
make sure you have the right virtual environment and try this :
python manage.py runserver
UPDATE : check if this is useful for you
I'm trying to start a project in Django, but I've come up against a wall right at the start. Whenever I run django-admin with no commands, I get this notice after the help message:
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
And whenever I try to run it or manage.py with a command such as runserver, I get:
python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 62, in execute
super(Command, self).execute(*args, **options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 73, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I don't know how to define this DJANGO_SETTINGS_MODULE nor where to call settings.configure(). I've tried what some other answers here suggested, but either it's a different problem, or I'm missing something that should be quite obvious.
Near the top of your manage.py file, you should see this line, where project is the name of your project which contains the settings file.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
If the project does not exist, or the path is wrong, python manage.py will fail.
The file tree should look something like this
.
├── manage.py
├── project
│ ├── settings.py
For context, a generated manage.py contains the following lines
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
# ... other code ...
You may use the suggested solution of an env variable in the exception - DJANGO_SETTINGS_MODULE.
export DJANGO_SETTINGS_MODULE="project.settings"
I have an old DJango application that used work fine but suddenly crashes. I use to test it with:
cd /opt/formshare/src/formshare/
export PYTHONPATH=$PYTHONPATH:/opt/formshare/src/formshare
export DJANGO_SETTINGS_MODULE=formshare.settings.default_settings
python manage.py validate
But now I get this error:
(formshare) bash-4.3$ python manage.py validate
Your environment is:"formshare.settings.default_settings"
Traceback (most recent call last):
File "manage.py", line 21, in <module>
execute_from_command_line(sys.argv)
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/opt/formshare/lib/python2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/opt/formshare/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/opt/formshare/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/opt/formshare/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'formshare.settings.default_settings' (Is it on sys.path? Is there an import error in the settings file?): cannot import name OrderedDict
I am using:
virtualenv=15.0.2
Python=2.7.11
amqp==2.1.1
billiard==3.3.0.23
celery==3.1.15
dict2xml==1.3
Django==1.6.11
django-braces==1.10.0
django-celery==3.1.16
django-cors-headers==0.13
django-db-readonly==0.3.2
django-digest==1.13
django-filter==0.7
django-guardian==1.2.4
django-nose==1.4.2
django-oauth-toolkit==0.7.2
django-registration-redux==1.1
django-reversion==1.8.4
django-taggit==0.12.1
django-templated-email==0.4.9
djangorestframework==2.4.3
djangorestframework-csv==1.3.3
dpath==1.2.post70
elaphe==0.5.6
FormEncode==1.3.1
gdata==2.0.18
httmock==1.2.2
httplib2==0.9
jdcal==1.3
jsonfield==0.9.23
kombu==4.0.0
librabbitmq==1.5.2
linecache2==1.0.0
lxml==3.4.0
Markdown==2.5
mock==1.0.1
modilabs-python-utils==0.1.5
nose==1.3.7
numpy==1.11.2
oauthlib==2.0.1
openpyxl==2.0.5
pandas==0.12.0
Pillow==2.5.3
poster==0.8.1
psycopg2==2.5.4
pybamboo==0.5.8.1
pylibmc==1.3.0
pymongo==2.7.2
python-dateutil==2.6.0
python-digest==1.7
-e git+https://github.com/qlands/python-json2xlsclient.git#5a39387752d819cb6387f75569dbea9a5288aa6f#egg=python_json2xlsclient
pytz==2014.7
-e git+https://github.com/XLSForm/pyxform.git#cfe8589f40319fa3279b0a83e0d23d49bcbe8408#egg=pyxform
recaptcha-client==1.0.6
requests==2.4.1
savReaderWriter==3.4.2
simplejson==2.6.2
six==1.10.0
South==1.0
traceback2==1.4.0
unicodecsv==0.9.4
unittest2==1.1.0
vine==1.1.3
xlrd==0.9.3
xlwt==0.7.5
I tried to add set the path and setting in the validate but nothing works.
Any idea what could have changed and how to correct it?
The django process doesn't appear to be seeing your formshare.settings.default_settings. Without seeing the project structure, it's hard to know exactly what's going on, but a few things you might check.
If you are using python 2.x, does the formshare/ and the settings/ directory both have an __init__.py file in them?
Try adding /opt/formshare/src/ to the python path instead of (or in addition to) what you have.
One trick that I often try to make sure the python path is correct: while I'm active in my virtualenv, I'll enter a python shell (not django shell) and try something like the following:
import formshare
from formshare import settings
from fomrmshare.settings import default_settings
The error messages that might be generated from those might also help isolate the issue.
Since you activated virtualenv, you have to do something different. Try add2virtualenv in virtualenvwrapper.For more info
http://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html#add2virtualenv
I'm trying to deploy my Django project using Heroku, but now stuck with the empty SECRET_KEY problem when I run the heroku run python manage.py syncdb command:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 338, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 190, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 4, in
from django.contrib.auth import get_user_model
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/init.py", line 7, in
from django.middleware.csrf import rotate_token
File "/app/.heroku/python/lib/python2.7/site-packages/django/middleware/csrf.py", line 14, in
from django.utils.cache import patch_vary_headers
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/cache.py", line 26, in
from django.core.cache import caches
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/cache/init.py", line 34, in
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 48, in getattr
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 113, in init
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
However, running python manage.py syncdb is totally fine. In my settings.py file, I even wrote the SECRET_EKY explicitly as:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '*$_99!t5u&#w&9boeq()=obq#rjdsfasdfa#8hp_5s%h-mh(hk'`
I still got the same problem. I wondered if the environment variable DJANGO_SETTINGS_MODULE was missing, but running heroku config gave me (I replaced the information with xxx):
DATABASE_URL: xxx
DJANGO_SETTINGS_MODULE: xxx
SECRET_KEY: xxx
It seems that the relevant environment variables do exist. I really hope you could give me some suggestions. Thank you very much in advance.
I faced this issues a while ago. The following command resolved the issue for me
command: heroku config:set SECRET_KEY="your_secret_key"
Might be a stupid question, but are you running heroku run python manage.py syncdb?
If so, I am very confident, that your settings module is not loaded.
This could potentially have one of the following reasons:
Your settings module is not where Django expects it
(but you are saying manage.py can find it)
You have another config, that overshadows your config settings
(for example a local.py settings module for development settings)
Did you do any modification to manage.py or wsgi.py?
I edit this answer if I can think of more.
You can as well go to your app on your heroku dashboard, click on settings, then, scroll downwards; you'll find the button Reveal Config Vars. Add the SECRET_KEY and it's value as instructed. You can as well, add other secret properties required by your application.
I've read a lot of similar questions, and I've spent a lot of time trying to work this out myself, but I'm still stuck. Here's my situation:
I'm going through the Django guide on Heroku (https://devcenter.heroku.com/articles/django). I originally started in windows, but after having all sorts of problems I decided it would be a lot easier to switch to linux. I wiped my hard drive and installed Ubuntu 12.10 and Win 7 side by side to dual boot. Everything has been installed with pip (again, following the Heroku guide). I'm using PostgreSQL since it is (supposedly) well-integrated with Heroku. Before I get into what I've tried, here is the error (it's from near the end of the tutorial):
(venv)wtodom#wubuntu:~/hellodjango$ python manage.py syncdb
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
If I try to specify the settings file with its absolute path to make sure it uses the right one I get this:
(venv)wtodom#wubuntu:~/hellodjango$ python manage.py syncdb --settings=/home/wtodom/hellodjango/hellodjango/settings.py
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/core/management/sql.py", line 6, in <module>
from django.db import models
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/home/wtodom/hellodjango/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings '/home/wtodom/hellodjango/hellodjango/settings.py' (Is it on sys.path?): Import by filename is not supported.
A lot of the similar questions had people editing the wrong settings.py file. I know I edited the right one. Here's a cat of it (on gist since it's kinda long) - https://gist.github.com/wtodom/4736303. I've tried every combination I can think of for the data in there, including empty strings - no difference.
Other posts I've read have had people who had issues with the Django version they were running. Mine is a fresh install through and through, so it's not that.
Some people had issues with Django not being on the path. If I open a python shell from the projects root directory I can import things, so it's not that.
Some people didn't have values in their settings.py file, but as you can see from the gist, I do. I also tried running the command (python manage.py syncdb) both with and without pgadmin running and logged in.
On that note, here is a screencap of pgadmin showing that I have databases created and up: http://i.imgur.com/CXD2AWo.png
Here's what I get when I \l in PostgreSQL (another gist since SO killed the formatting): https://gist.github.com/wtodom/4736359
While I'm at it, here's a \du listing: https://gist.github.com/wtodom/4736442
I made sure that my manage.py has os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings") in it.
Everything in the Heroku tutorial worked without a hitch in Ubuntu up until I tried to sync the db locally (python manage.py syncdb). I can run the same command through heroku and it works fine (heroku run python manage.py syncdb).
I'm sure I'd forgotten probably something relevant that I've tried or read. I've spent about 6 hours reading and trying to get this thing going, just on this one error. I don't really know what else to try.
Also, I'm really new to databases and non-.NET web development (I've worked on projects, but never set one up), so if you know what's wrong with my setup please try to be clear. Thank you very much!
Heroku and your local machine usually need different database settings. A common way to handle that is to first list your local database settings in the settings.py file, then do a check to see if you're code is currently running on Heroku, and if so, override your database settings with what Heroku needs. That allows a single settings.py file to work in both environments.
Heroku creates an environment variable called DATABASE_URL containing the database URL string. A common way to check whether your code is running on Heroku is to check for that environment variable. Then, if it exists, use the dj_database_url module, which knows how to parse that string into the dictionary that Django needs for its configuration.
The only thing incorrect in your code is that you didn't check for the environment variable first before calling dj_database_url.config(). This means that dj_database_url, which normally should only run within Heroku, was running on your local machine too. Since your local machine almost certainly doesn't have the DATABASE_URL environment variable set, dj_database_url returned an empty dictionary. So, your code is overwriting your local database configuration with an empty dictionary when it's not run on Heroku. Django looks in that dictionary for the information it needs, can't find it, and raises an error.
To fix the problem, add a check for DATABASE_URL before using dj_database_url.
Change this part at the end of settings.py:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
To this:
if os.environ.get('DATABASE_URL', None):
import dj_database_url
DATABASES['default'] = dj_database_url.config()