Django with MySQL install on Mac

I've been trying to get setup with a good work flow for some Django projects I've been working on and have noticed a lack of google search how-tos for it. This is a brief tutorial on how to get everything you need set up for a local copy of Django on OS X 10.5 or higher. On the web side of things I've been coming from an Apache-MySQL-PHP background where installing MAMP makes local development a breeze. What this tutorial sets up is a Django installation on top of the Python already installed on your Mac and a MySQL server. We don't need to mess around with a web server because Django has a development one built in.

1.) Download the correct DMG for your computer from this website http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg (I'll be using mysql-5.1.47-osx10.6-x86_64.dmg)

2.) Open it up and install mysql-versionNums.pkg You probably don't want mysql to auto start up with your computer so don't install MySQLStartupItem.pkg

3.) Now MySQL is installed and you need to connect it to the python installation that comes on your Mac. Grab the program http://sourceforge.net/projects/mysql-python/files/ and unpack it.

4.) In the directory you just unpacked, edit a file called setup_posix.py and change the line


mysql_config.path = "mysql_config"  
to  
mysql_config.path = "/usr/local/mysql/bin/mysql_config" 

5.) Open up the Terminal and navigate into the MySQL-python directory and type the following commands. These will build this project and install it for you.


python setup.py clean  
python setup.py build  
sudo python setup.py install 

6.) Python and MySQL are now happily installed together and we need to drop Django on top of that. Download the latest version of Django here http://www.djangoproject.com/download/

7.) Follow the terminal commands as described on the page linked above to install. I'll repost them here.


tar xzvf Django-1.2.1.tar.gz
cd Django-1.2.1
sudo python setup.py install

8.) Now we need to setup the mysql server with a database for the project. In the Terminal run:


./usr/local/mysql/bin/mysql -u root -p

When prompted for a password simply leave it blank and hit enter. Since this is a local server it is ok to leave the password blank.

9.) Making the database itself is a simple command. I will be using 'project_name' but obviously use whatever fits best with your project.


create database project_name;
exit;

10.) Now its time to actually build the project with Django's startproject command.


django-admin.py startproject project_name

This will create a folder with some pre-made python files that will get you started.

11.) Now we hook your project up to your database. Open the settings.py file that was just created. In this you will change this:


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

to this:


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'rockport',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

12.) Now fire up Django's dev server from your project directory with:


python manage.py runserver

Point your browser at http://127.0.0.1:8000 and you will see Django's "Congratulations, now build something" welcome screen.

After this you should be ready to start developing some Django apps. The documentation on Django's site is very good and they have some awesome tutorials. I highly recommend walking through http://docs.djangoproject.com/en/1.2/intro/tutorial01/#intro-tutorial01 to get a feel for how the framework manages things. Enjoy!