gglasses
gglasses

Reputation: 876

How to choose Django config setting in virtualenv just once

I was reading some materials to better organize my code in Django and I started reading this: http://www.revsys.com/blog/2014/nov/21/recommended-django-project-layout/ ... Thought it was a good idea to have different config-settings for production and development, mostly because of databases.

After that in the post it's explained that we can do one of two things to select the config-setting we want:

export DJANGO_SETTINGS_MODULE="foo.settings.production" or

./manage.py migrate —settings=foo.settings.development

And I thought the first option is better... so every time I initialize my virtualenv, before I run python manage.py runserver I need do: export DJANGO_SETTINGS_MODULE="foo.settings.development"...

Hmm, I believe if I edit my ~/.bashrc and include export DJANGO_SETTINGS_MODULE="foo.settings.development" I won't have to do it again. But this will shared across all my virtualenvs and I don't want this.

To summarize: I want to define export DJANGO_SETTINGS_MODULE="foo.settings.development" just once in my virtualenv and to keep it there. Independently (that's the point of virtualenvs, right)...

Upvotes: 0

Views: 177

Answers (1)

user3148949
user3148949

Reputation: 578

If you are not using virtualenvwrapper, you can put it in $VIRTUAL_ENV/bin/activate script. like

export DJANGO_SETTINGS_MODULE='foo.settings.development'

In the same script you can see a deactivate function, there you can unset the environment variable, which will be called while deactivating virtualenv

unset DJANGO_SETTINGS_MODULE

If you are using virtualenvwrapper you can set environment variable in postactivate and unset it in postdeactivate scripts

Upvotes: 1

Related Questions