Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make the Django database path (for sqlite3) "cross-platform"?

I'm in the process of learning Django and Python (as well as programming in general). For the sake of simplicity, I am using sqlite3 as my database while I'm going through tutorials for Django and such.

I am a multi-platform user (Mac OS, Windows, Linux) depending on where I am at the time. So, what I have done is put my programming projects in my Dropbox so that I can work on the same code from anywhere.

The problem is that, in the settings.py file for a particular project, I specify the database path like so:

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.sqlite3',                             # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'C:/Users/David/Dropbox/programming/mysite/database',       # Or path to database file if using sqlite3.

... but when I'm using MacOS or Linux, obviously the C:/ chokes. I was wondering if someone had a suggestion of a simple remedy to this. Of course, one way I could do it would be to set up my database remotely on my webserver via MySQL or something, but I thought there might be a simple way to do it such as with an 'if' statement.

like image 906
David Michael Avatar asked Jun 04 '12 22:06

David Michael


1 Answers

Using relative paths in settings.py is a common enough to be considered a best practice by many. Something like this may help.

from os.path import dirname, join

PROJECT_DIR = dirname(__file__)

DATABASES = {
    # ...
    'NAME': join(PROJECT_DIR, 'your_db_name.db'),
    # ...
}
like image 107
istruble Avatar answered Sep 24 '22 11:09

istruble