13

I want the environment variable DJANGO_SETTINGS_MODULE to change depending on what I say for workon. It seemed to me that I want to set it in .virtualenvs/postmkvirtualenv but my trial had no effect.

ftpmaint@millstone:~$ cat ~/.virtualenvs/postmkvirtualenv
#!/bin/bash
# This hook is run after a new virtualenv is activated.
export DJANGO_SETTINGS_MODULE=newproject.settings
ftpmaint@millstone:~$ echo $DJANGO_SETTINGS_MODULE
az.settings
ftpmaint@millstone:~$ workon newproject
(newproject)ftpmaint@millstone:~$ echo $DJANGO_SETTINGS_MODULE
az.settings

Could someone set me straight; where should I put that export?

In addition, will it restore when I deactivate? And if not, is there some natural way to restore it?

Jim Hefferon
  • 438
  • 2
  • 4
  • 11
  • You can do this: http://stackoverflow.com/questions/8826534/how-can-i-correctly-set-django-settings-module-for-my-django-project-i-am-using/22452281#22452281 – zzart Mar 17 '14 at 10:49

2 Answers2

22

One way I've done that before is by appending an export statement to the end of ./bin/activate

export DJANGO_SETTINGS_MODULE="myproject.settings"
Joe J
  • 9,985
  • 16
  • 68
  • 100
14

You were on the right track, but you want to use the postactivate hook instead of postmkvirtualenv.

It won't restore automatically on deactivate. Thankfully there is postdeactivate hook that you can use to manually restore any environmental variables that you changed on activate.

mmcnickle
  • 1,599
  • 10
  • 13
  • Thank you (to both answerers). The key was "activate", and I didn't understand that before. – Jim Hefferon Sep 29 '11 at 19:55
  • Isn't the postactivate hook common for all environments? I think there is not much point of setting `DJANGO_SETTINGS_MODULE` there. – Petr Peller Dec 18 '13 at 18:09
  • 4
    There's both a global and local version of the postactivate hook. For a local-only setting like this, you'd want to add the `export` to `$VIRTUAL_ENV/bin/postactivate` rather than the global `$VIRTUALENVWRAPPER_HOOK_DIR/postactivate`. – mmcnickle Dec 24 '13 at 00:38