Environment Setup For Django@1.6.11 + Python@2.7 + MySQL@5.x

1. Prerequisite

  1. install Python 2.7
  2. install MySQL 5.x

2. Install Django 1.6.11

  1. Official website: https://www.djangoproject.com/download/
  2. Use command line:
pip install Django==1.6.11
  1. Verify what you have installed:
python -c "import django; print(django.get_version())"
  1. To see where Django was installed:
python -c "import sys; sys.path = sys.path[1:]; import django; print(django.__path__)"

3. Create a Django project

  1. Create with command line:
django-admin.py startproject mysite

Here mysite is an example of the project’s name.

  1. Directory tree:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py

4. Install the PyMySQL package

  1. Use command line:
pip install PyMySQL
  1. Paste the following code to the __init__.py file, which was left empty after project creation, just to let Python know that this is a Python package:
import pymysql

pymysql.install_as_MySQLdb()

5. Set up the setting.py file

  1. Change the DATABASES part into the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite_db',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'yourpassword',
}
}
  1. Verify with:
python manage.py syncdb

The output should be something like:

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'XXX'): admin
Email address: admin
Error: Enter a valid email address.
Email address: XXX@company.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

The syncdb command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file.
You’ll see a message for each database table it creates, and you’ll get a prompt asking you if you’d like to create a superuser account for the authentication system. Go ahead and do that.

6. Use your own models to create MySQL tables

  1. Notice that Django cannot create a database for you, but tables only, so we need to create a DB directly in MySQL first.
mysql -u root -p
password:
CREATE DATABASE mysite_db;
USE mysite_db;
  1. Create an app with Django:
python manage.py startapp myapp

This command would create a directory myapp under the root container, at the same level with manage.py:

myapp/
__init__.py
admin.py
models.py
tests.py
views.py
  1. Django would create a MySQL table for each designed model/object. Following is the Sample model code can be pasted into the models.py file:
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __unicode__(self):
return self.question

7. Generate models from existing tables

With command inspectdb to see how Django parse the MySQL tables:

python manage.py inspectdb

If pending with > models.py, we can get some auto-generated models in the models.py file.

8. Bind to gunicorn

pip install gunicorn
gunicorn --bind 0.0.0.0:8000 mysite.wsgi