Cookiecutter-django: Reading env settings from file using environ leads to crash to some applications like sphinx, django extensions

Created on 13 Aug 2018  路  14Comments  路  Source: pydanny/cookiecutter-django

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 variablepath_to_settingsshould be"config.settings.local". However, by runningmake htmlthis will generate error because functionenv()` 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

  • generate .rst files from your apps.
  • within \doc\ directory run make html

Most helpful comment

@Micromegass Glad I was able to help! Cheers.

All 14 comments

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.

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

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

Glad to hear that :)

@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

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

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

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?

@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

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?

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

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

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!!

@Micromegass Glad I was able to help! Cheers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

japrogramer picture japrogramer  路  4Comments

jayfk picture jayfk  路  4Comments

huydbui picture huydbui  路  3Comments

StupidTAO picture StupidTAO  路  3Comments

bluesurfer picture bluesurfer  路  4Comments