Cookiecutter-django: django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

Created on 13 Sep 2020  路  6Comments  路  Source: pydanny/cookiecutter-django

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/environ/environ.py", line 273, in get_value
    value = self.ENVIRON[var]
  File "/usr/local/lib/python3.8/os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'DATABASE_URL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/manage.py", line 31, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 67, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "/usr/local/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.8/site-packages/django/conf/__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/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 "/app/config/settings/local.py", line 1, in <module>
    from .base import *  # noqa
  File "/app/config/settings/base.py", line 44, in <module>
    DATABASES = {"default": env.db("DATABASE_URL")}
  File "/usr/local/lib/python3.8/site-packages/environ/environ.py", line 204, in db_url
    return self.db_url_config(self.get_value(var, default=default), engine=engine)
  File "/usr/local/lib/python3.8/site-packages/environ/environ.py", line 277, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

the errors happend when pycharm runserver, how to solve this?

question

All 6 comments

Hi,
Did you export your DATABASE_URL from your terminal before running your command?
Maybe this will fix your issue ->

Set the environment variables for your database(s):

$ export DATABASE_URL=postgres://postgres:<password>@127.0.0.1:5432/<DB name given to createdb>
#Optional: set broker URL if using Celery
$ export CELERY_BROKER_URL=redis://localhost:6379/0

Ref -> https://cookiecutter-django.readthedocs.io/en/latest/developing-locally.html

@EVENFATE This may be due to the docs service. Check #2674. It seems like you're using Docker...(?) so if you do those exports, it wouldn't help.

@Andrew-Chen-Wang

Hi, for me he does not seems to run docker, or we can't know yet from the actual logs,
as it is mentioned, the error is from the fact that environ package (at the beginning of. base.py ) raise a key value error.

File "/usr/local/lib/python3.8/site-packages/environ/environ.py", line 273, in get_value value = self.ENVIRON[var] File "/usr/local/lib/python3.8/os.py", line 675, in getitem raise KeyError(key) from None KeyError: 'DATABASE_URL'

which also can be see at the end of the logs:

File "/app/config/settings/base.py", line 44, in
DATABASES = {"default": env.db("DATABASE_URL")} # <==. key: value that are actually missing.

If an error regarding DOCKER should happen. It would be later, so "use export" can be a way around to run the full package, either from docker-compose or from within a python virtualenv.

Perhaps, if I remember well, base.py is calling from local.py in dev environment.
So, the first error likely to occur is DATABASE_URL, then CELERY_BROKER_URL (eventually if it was requested to be configure like so while initiating the project. and finally if there is an issue with docker you will be notified for nor running your app in Docker environment.

I was inferencing that OP was using Docker based on the traceback from this: File "/app/config/settings/local.py", line 1, in since we set the Dockerfile dir to be /app.

The bug fix that I filed is basically what you described: DATABASE_URL couldn't find the environment variable, and then you couldn't find CELERY_BROKER_URL. If you try to create a new cookiecutter-django project using Docker, you'll see that's exactly what errors from the docs service.

Yep you are right, i agree

Going to close this as the docs service has been fixed in #2920

Feel free to reopen or create a new issue if the problem persists in a freshly generated project.

Was this page helpful?
0 / 5 - 0 ratings