Last Updated: February 25, 2016
·
430
· rafshar

Setup a Django Project (1.4 or later)

  1. Create your virtualenv, install stuff.

    mkvirtualenv new_project
    pip install django
    pip install south          # db migration manager for django
  2. Start up a django project.

    mkdir new_project
    cd ./new_project
    django-admin.py startproject new_project

    Start project will generate the following:

    new_project/
        new_project/
            new_project/        # project-level items
            new_app/            
                __init__.py
                models.py
                tests.py
                views.py
            manage.py
  3. Setup local_settings.py for environment-specific settings (e.g. DB info, dev apps, etc...). Anything you place in local_settings.py will only run locally.

    Add the following to the bottom of settings.py

    try:
        LOCAL_SETTINGS
    except NameError:
        try:
            from local_settings import *
        except ImportError:
            pass

    Create a local_settings.py file in the same directory as settings.py starting with the lines below. All dev specific go here.

    LOCAL_SETTINGS = True
    from settings import *
    DEBUG = True
  4. Setup your databases.

    Copy DATABASES from settings.py into local_settings.py and enter the information.

    DATABASES = {
        'default': {
            ...
        }
    }

    Add south to INSTALLED_APPS in settings.py

    INSTALLED_APPS = (
        ...
        'south',
    )

    Perform a syncdb to create all of your initial datbaase tables.

    python ./manage.py syncdb
  5. Run the development server to make sure everyone's happy

    python ./manage.py runserver

    You should see a Django generated 404 error.

  6. Create a requirements.txt for project dependencies.

    pip freeze > requirements.txt
  7. Start creating apps!

    python ./manage.py startapp my_app

    Your project folder will now look as follows

    new_project/
        new_project/
            new_project/        # project-level items
            new_app/            
                __init__.py
                models.py
                tests.py
                views.py
            manage.py

    Add your app to INSTALLED_APPS in settings.py

    INSTALLED_APPS = (
        ...
        'south',
        'my_app',
    )

    Create and run a migration for your new app.

    python ./manage.py schemamigration --initial new_app
    python ./manage.py migrate new_app