cookiecutter / cookiecutter-django

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.
https://cookiecutter-django.readthedocs.io
BSD 3-Clause "New" or "Revised" License
12.15k stars 2.91k forks source link

Reading env settings from file using environ leads to crash to some applications like sphinx, django extensions #1747

Closed uzi0espil closed 6 years ago

uzi0espil commented 6 years ago

What happened?

First of all, I am not sure whether this ticket should be a bug report or an enhancement. Either case, whenever you want to generate docs using sphinx, sphinx should be able run the application. Therefore, for any django project the docs/conf.py should contains the following additional code:

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../'))
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", path_to_settings)
django.setup()

Using Cookiecutter template, the variable path_to_settings should be "config.settings.local". However, by running make html this will generate error because function env() won't be able to work and will raise the following error:

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

To fix, you can replace every instance of env(..) to the correct value for example: CELERY_BROKER_URL = env('CELERY_BROKER_URL') should be replaced with CELERY_BROKER_URL = "redis://redis:6379/0"

Therefore, I have created a new settings script just for documentation and it only contains the minimal settings to make django application run. I called the settings settings_docs.py and within \docs\conf.py, the
django settings module will be os.environ.setdefault("DJANGO_SETTINGS_MODULE", config.settings.settings_docs)

Steps to reproduce

uzi0espil commented 6 years ago

This problem also happens when you try to run some django extensions commands such as graph-models It will generate the following problem:

django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable. It seems these applications can't use environ to read from settings from file.

P.S: Since it is not sphinx related, I have renamed the title for more appropriate title.

webyneter commented 6 years ago

If using Docker, try docker-compose run command rather than docker-compose exec.

uzi0espil commented 6 years ago

Ya I am running it in docker.. Thanks your solution was quite helpful and it solved the problem of not reading settings from text.

webyneter commented 6 years ago

Glad to hear that :)

underchemist commented 5 years ago

@webyneter can you expand a bit on why the environment variables are exported properly when using run instead of exec? I would like to use exec and have DATABASE_URL set properly

Micromegass commented 4 years ago

@uzi0espil I have the same problem. Would you mind explaining how you solved it? I also use cookiecutter and docker.

uzi0espil commented 4 years ago

@Micromegass Simply just execute the following:

docker-compose -f local.yml run django python manage.py YOU_RCOMMAND.

Or to build documentation.

docker-compose -f local.yml run django make -C ./docs html

So to preserve the environment values, just use docker-compose's run command.

Micromegass commented 4 years ago

thanks for the quick reply. Trying this I get /entrypoint: exec: line 43: make: not found From which directory are you executing this? And is your documentation folder outside the django project or, like I have it, inside of it?

uzi0espil commented 4 years ago

@Micromegass I am running the command from the project's root folder. The docs folder sets in project's root. For example:

- Project
-- Project => where django code and its applications reside.
--- media
--- ....
-- docs
--- make => this what you need to set a parameter for -C
--- Makefile
--- ....
-- configs
-- ...
-- productions.yml
-- local.yml

you can try and set the full path to -C for example: /app/PROJECT_NAME/docs/make

Micromegass commented 4 years ago

thanks again. I have the exact same setup. No matter how I write this command it always says it can't find make. Are you sure the make html command is splitted before and after the -C flag?

uzi0espil commented 4 years ago

One thing I have different from the cookiecutter framework is that I am using python:3.6.6-stretch and not the alpine image for django. what I would suggest is to ssh inside the docker and from the root directory try to run make. check also this problem in alpine

Micromegass commented 4 years ago

Ok I will try that. thanks very much for your help!

Micromegass commented 4 years ago

Mate, I it worked. ssh'd into docker and turns out make wasn't installed. I added apk add make to my dockerfile, ran the command again and it worked. Thanks a bunch!!

uzi0espil commented 4 years ago

@Micromegass Glad I was able to help! Cheers.