0

background

I'm trying to split my settings by environment by following these instructions.

Now I would like to simply run my test command like so:

./run ./manage.py test --settings=bx.settings.local

currently the following line

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bx.settings")

is available in these files

manage.py
wsgi.py

and so i removed it (since it's supposed to be read from the command line instead).

I also created a settings folder inside my bx app and added the files

__init__.py
base.py
local.py

to it.


notes

note: the run file is this:

#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
docker run \
  --env "PATH=/beneple/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
  --link beneple_db:db \
  -v $DIR:/beneple \
  -t -i --rm \
  beneple/beneple \
  $@


problem

when i run the command

./run ./manage.py test --settings=bx.settings.local

I get this error

  File "/beneple/bx/org/serializers.py", line 10, in <module>
    from bx.settings import DOMAIN
ImportError: cannot import name DOMAIN

in serializers.py:10, we got this

from bx.settings import DOMAIN

so i replaced bx.settings with

from django.conf import settings
from settings import DOMAIN

and instead i got this error:

  File "/beneple/bx/org/serializers.py", line 12, in <module>
    from settings import DOMAIN
ImportError: No module named settings

debugging

the weird part is that if i put a breakpoint after from django.conf import settings, and type the following:

ipdb> print(settings)
<Settings "bx.settings.local">
ipdb> settings.DOMAIN
'http://localhost:8000'

I'm confused why it's not recognizing settings here as a module?


update

I noticed that my templates directory changed. In my settings file I have

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
..
TEMPLATES = [{
    'DIRS': [os.path.join(BASE_DIR, 'templates')],..

However notice the difference in the value of settings.TEMPLATES[0]['DIRS'] between the old way and new way:

old way:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bx.settings")
['/beneple/templates']

new way:

./run ./manage.py test --settings=bx.settings.local
['/beneple/bx/templates']

why is this the case? and how do I (programmatically) make the new way output the same result as the old one?

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246
  • You don't import settings directly, but access them like this from `django.conf import settings` then `settings.DOMAIN`. – Klaus D. Jul 29 '16 at 09:05

2 Answers2

2

Once you've imported settings from django.conf, you mustn't import again from settings; you already have the settings object, you can just refer to settings.DOMAIN directly.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • that's interesting.. but then why did it work before when i had `from bx.settings import DOMAIN`? did it treat `bx.settings` as an module rather than an object? and if so.. why? – abbood Jul 29 '16 at 09:12
  • No that has nothing to do with it and I'm not sure what you're confused about. Before you imported DOMAIN directly from the module; but now you're attempting to import the module, then reference that module again in an import statement. – Daniel Roseman Jul 29 '16 at 09:14
  • ok i made an update to my question (re templates).. last update i promise! – abbood Jul 29 '16 at 09:54
  • `TEMPLATES = [{ 'DIRS': [os.path.join(os.path.dirname(BASE_DIR), 'templates')],...` – Azimkhan Jul 29 '16 at 11:19
1

from settings import DOMAIN tries to load module settings from PYTHONPATH, not from the module you already imported.

You could just do the following: DOMAIN = settings.DOMAIN

Azimkhan
  • 330
  • 3
  • 7