Reputation: 13
I'm new to Python and Django and have over the past few weeks managed to set up my first deployment - a very basic site with user authentication and a few pages, which I hope to fill with content in the next couple of weeks.
I have managed to find the answer to probably 40+ questions I have encountered so far by searching Google / StackOverflow / Django docs etc., but now I have one I can't seem to find a good answer to (perhaps because I don't know how best to search for it): when I develop on my local machine I need my settings.py file to point to the remote database ('HOST': 'www.mysite.com',
) but when I deploy to a shared hosting service provider they require the use of localhost
('HOST': '',
in settings.py).
Since I host my code on GitHub and want to mirror it to the server, is there a way to resolve this so I don't have to make a manual edit to settings.py each time after uploading changes to the server?
Upvotes: 1
Views: 179
Reputation: 1754
You can create a local_settings.py file which you can put it in .gitignore and include it in the original settings.py. Put every environment sensitive variable in this file (DATABASES, DEBUG, CACHES, MEDIA_URL etc.)
In settings.py:
try:
from local_settings import *
except ImportError:
pass
In .gitignore:
*/local_settings.py
I recommend you to put a local_settings.py.example next to the setting files which will be in the gitrepo. This way any time you deploy to a new environment you just need to copy this example and change the necessary lines.
Upvotes: 0
Reputation: 174624
You can have two copies of your settings.py
file, one for production and one for development. Whatever you need to be the default, name it as settings.py
Just set DJANGO_SETTINGS_MODULE
to the python path for the file that you would like to use.
So, if your settings files are myproject/settings.py, myproject/settings_dev.py
; you can then do:
$ DJANGO_SETTINGS_MODULE=settings_dev python manage.py shell
From the myproject
directory.
Upvotes: 1