3

In my Django project, I want the settings.py file to be included in the initial commit so that other developers can clone the repository and change the original settings to work with there local setup. But, after that I don't want to manually exclude the file at every commit, and the ignore file wont work because it's already tracked.

So, how can I get mercurial to automatically exclude the settings.py while maintaining the original file tracked?

Appreciate any help :)

Sami
  • 531
  • 1
  • 5
  • 3
  • Once Mercurial tracks a file, it always tracks that file. The only way to avoid Mercurial committing changes to it is to specifically ask it not to, on each commit. – Lasse V. Karlsen Jan 22 '11 at 16:31

3 Answers3

9

There is an alternative, probably more flexible approach to this. Don't exclude the settings.py file.

Instead, do something like this:

try:
  from localsettings import *
except ImportError:
  pass

That way, if you ever need to actually change your settings.py file, others can pick up the changes. You can supply reasonable defaults, so long as any settings you may want to override locally are above the localsettings import.

You can change your localsettings.py file as much as you need (I tend to use it for turning on DEBUG, setting my database settings, and making sure I'm just using local memory as my cache backend).

Just make sure you exclude the localsettings.py file from your commits.

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
2

The way this is most generally done with Mercurual is to commit a file called something like settings.py.template, and have the first step someone takes on a new clone be to copy that over to a settings.py file, which is listed in the .hgignore file. That way, if someone adds a new settings in the template people will still get it and can merge it into their local settings. It's easy to automate the copying of template to actual in your start/deploy script or using a clone hook, though most folks don't bother.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
0

I know this is an old thread, but others might find a different approach still useful. (I came across this SO asking the same question).

You can keep settings.py in the repository and modify it to get machine specific values from enviornment variables. this is suggested here and here

As example would be the DB connection that often differ from one machine to another:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',  
        'NAME': os.environ['DB_NAME'],                      
        'USER': os.environ['DB_USER'],
        'PASSWORD': os.environ['DB_PASSWORD'],
        'HOST': os.environ['DB_HOST'],                      
        'PORT': os.environ['DB_PORT'],                      
    } }
Community
  • 1
  • 1
rexposadas
  • 3,109
  • 3
  • 27
  • 49