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

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?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 21 (7 by maintainers)

Most upvoted comments

@TElphee01 Correct, it is set in entrypoint under the compose/django directory. If you want to run a command like shell, you’ll need to do docker-compose -f local.yml run --rm django python manage.py shell

@habenhkt are you aware that you are asking a question in a closed Issue? Besides that, the issue section of github is for issues concerning the project. Your problem is more suitable for in QA site like stackoverflow or the discussion section.

Regarding your question, check the env() method, it is missing the keyword default (settings.py, line 293).

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

the compose file includes an entry point which the dockerfile does not include. What you could do is add entrypoint stuff to your start file. The entrypoint file just ensures all necessary services like Postgres is running, but it also includes setting a DATABASE_URL var which is crucial

@Andrew-Chen-Wang Yes sir that did work as well! Thank you for your help!

Yep you are right, i agree

@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.