Setting DJANGO_SETTINGS_MODULE under virtualenv?

2019-03-13 01:51发布

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?

2条回答
成全新的幸福
2楼-- · 2019-03-13 02:38

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"
查看更多
Anthone
3楼-- · 2019-03-13 02:45

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.

查看更多
登录 后发表回答