Your comments are welcome! See the included form near the bottom of this page.
|

|
Preparation
|
|
|

|
Find an available domain name
|
|
|
|
|
Use gandi.net's to simultaneously test up to 50 names with a variety of TLDs
|
|
|

|
Register the domain name
|
|
|
|

|
Find a web template
|
|
|
|
|
Many sources for this. You can find good ones in "Free HTML layouts" type of articles on smashingmagazine.com
|
|
|

|
Keep a TODO file running with all of the features you want to include in your project. A good way to keep motivated is to order the features by importance, and settle on implementing the first 5 most important features for version 0.1, deploying your project to the server, then develop 5 new features, deploy them, etc.
|
|
|

|
Development preparation
|
|
|

|
Download and install Django on your local, development machine
|
|
|

|
Create a directory to store the project
|
|
|
|
|
A good place to keep your web work tidy, under OS X, is ~/Sites/<domain name>/ -- where you might want to create a "trunk" folder for example.
|
|
|

|
Open up a terminal window, then cd to the project directory created above
|
|
|

|
Create the Django project by executing: "django-admin.py startproject <MYPROJECT>" replacing < MYPROJECT >
|
|
|
|
|
Keep your project's name short and concise. You will be typing it many times
|
|
|

|
We will now add a project-wide constant so that your project can "sense" if it's running on your development machine, or your deployment server
|
|
|
|
|
We assume that you have a single development machine and a single server
|
|
|

|
Open a terminal window on your development machine
|
|
|

|
Execute "python"
|
|
|

|
In the Python interactive command-line, type: "import os" then enter
|
|
|

|
In the same command-line, type "os.uname()[1]" then enter. This is one of your development machine's names. Copy it to your clipboard
|
|
|

|
Edit settings.py in the following way:
|
|
|

|
Add "import os" to the top of the file
|
|
|

|
Right after the "import os" line, add the following line:
|
|
|

|
IS_SERVER = (os.uname()[1] != "<YOUR-DEV-MACHINES-NAME>") replacing <YOUR-DEV-MACHINES-NAME> with your development machine's name, found previously
|
|
|

|
In settings.py, under the IS_SERVER declaration, add:
|
|
|

|
if IS_SERVER: PROJECT_ROOT = '' else: PROJECT_ROOT = '<PATH-TO-DJANGO-PROJECT-DIRECTORY>' replacing PATH-TO-DJANGO-...
|
|
|

|
Leave the first declaration (PROJECT_ROOT = '') empty for the moment. We will fix this when deploying to the server
|
|
|

|
Make sure that your second PROJECT_ROOT declaration (the one with the actual project directory path) ends with a slash
|
|
|
|
|
Example: PROJECT_ROOT = '/Users/paul/Sites/example.com/trunk/mynewsite/'
|
|
|

|
Edit settings.py -- for development purposes, set the following:
|
|
|

|
DATABASE_ENGINE = 'sqlite3'
|
|
|

|
DATABASE_NAME = PROJECT_ROOT + '/main.db'
|
|
|

|
Open a terminal window, then cd into the Django project directory
|
|
|

|
Execute: "python manage.py syncdb"
|
|
|

|
Create a superuser and set its password
|
|
|

|
Create an application for your Django project
|
|
|
|
|
Your first and main application might be named "core", although this is a matter of taste
|
|
|

|
Execute: "python manage.py startapp <MYAPPLICATION>" replacing <MYAPPLICATION>
|
|
|

|
Edit settings.py and add "<MYPROJECT>.<MYAPPLICATION>" to INSTALLED_APPS, replacing both <MYPROJECT> and <MYAPPLICATION>
|
|
|
|
|
For example, you might add 'mynewsite.core' to the list. Don't forget to add a comma at the end of the new line you've just inserted
|
|
|

|
Create a "templates" directory in your Django project directory, then:
|
|
|

|
Edit settings.py in the following ways:
|
|
|

|
Delete the 3 lines of comments present in the TEMPLATE_DIRS declaration
|
|
|

|
Change the variable declaration to read:
|
|
|

|
TEMPLATE_DIRS = (PROJECT_ROOT + 'templates',)
|
|
|

|
Create a "static" directory in your Django project directory, then:
|
|
|

|
Edit urls.py and add "from django.conf import settings" to the top
|
|
|

|
Edit urls.py and add after the initial "urlpatterns" declaration:
if settings.DEBUG: urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.PROJECT_ROOT + 'static'}), )
|
|
|

|
Optionally, use "south", a database migrations manager. Download and install it on your development machine
|
|
|
|
|
For most projects (which will inevitably see database structure changes, as your project progresses), this is a very elegant and useful package. See south.aeracode.org
|
|
|

|
Edit your settings.py and add 'south' to INSTALLED_APPS. Don't forget to add a comma at the end of the line you've added.
|
|
|

|
Execute: "python manage.py syncdb"
|
|
|

|
Once you've created some models, execute: "python manage.py startmigration <MYAPPLICATION> --initial" replacing <MYAPPLICATION>
|
|
|

|
Optional: Data import
|
|
|
|
|
Are you importing any initial data into your site's database? If so, follow these steps
|
|
|

|
Create a standalone Python script from which you'll be able to access your Django project. Base it on the following:
|
|
|

|
path_prefix = '<PATH-TO-DJANGO-PROJECT-PARENT>'
import sys, os sys.path.append(path_prefix + '<MYPROJECT>') sys.path.append(path_prefix)
from django.core.management import setup_environ from <MYPROJECT> import settings setup_environ(settings)
|
|
|

|
In the code given above, replace <PATH-TO-DJANGO-PROJECT-PARENT> and both <MYPROJECT> with the appropriate values
|
|
|
|
|
For example, path_prefix = ''/Users/paul/Sites/example.com/trunk/' and sys.path.append(path_prefix + 'mynewsite')
|
|
|

|
Once you have the code above at the beginning of your standalone import script, you can then import your application's models, create new database object instances, and save them to the database
|
|
|
|
|
For instance, you might have
from mynewsite.core.models import mytable
f = open('data.txt') for line in f: new_obj = mytable() new_obj.value = line new_obj.save()
|
|
|

|
Development
|
|
|

|
Execute: "python manage.py runserver"
|
|
|

|
Open a browser window and point it to 127.0.0.1
|
|
|

|
Edit <MYPROJECT>/urls.py
|
|
|
|

|
Edit <MYPROJECT>/<MYAPPLICATION>/models.py
|
|
|
|

|
Edit <MYPROJECT>/<MYAPPLICATION>/views.py
|
|
|

|
Add any static files (e.g., JavaScript or image files) under the <MYPROJECT>/static/ directory
|
|
|

|
Add your templates under <MYPROJET>/templates/
|
|
|
|
|
it is good practice to have a base template named "base.html" which you'll re-use in other templates by way of "{% extends "base.html" %}" Template tag reference: docs.djangoproject.com—builtins
|
|
|

|
Create "404.html" and "500.html" templates which will be used in case of "Page not found" and "Server error" messages
|
|
|

|
Are you including any hard-coded UTF-8 strings in any of your Python code? If so, place the following in the first or second line of your script:
|
|
|

|
# -*- coding: utf-8 -*-
|
|
|

|
Deployment
|
|
|

|
Configure your domain name to point to your deployment server's IP address
|
|
|
|
|
Django is easier to deploy when you're running your project on a standalone server (instead of say, a shared web hosting package). I like linode.com's virtual servers - cheap and flexible.
|
|
|

|
Download and install Django on the deployment server
|
|
|
|
|
Did you install and use any additional Django packages? Download and install them too.
|
|
|

|
If you are using "south", download and install it as well on your deployment server
|
|
|

|
Create a parent directory in which you will create the Django project directory
|
|
|
|
|
Example: "/var/www/example.com/trunk/" under which you'll create the "mynewsite" Django project directory
|
|
|

|
Copy your project's files over.
|
|
|

|
If you intend to start a clean site (e.g., keep the development and deployment dabatases separate), don't copy "main.db" over to the server
|
|
|

|
If you did not copy main.db, run "python manage.py syncdb"
|
|
|

|
If south installed and/or migrations were done on development side, run "python manage.py migrate"
|
|
|

|
Find your Django project's directory path ("cd" into the directory, then execute "pwd")
|
|
|

|
Edit settings.py and set the PROJECT_ROOT declaration (the first one in the "if IS_SERVER:") to your Django project's directory path.
|
|
|
|
|
This might now look like this:
if IS_SERVER: PROJECT_ROOT = '/var/www/example.com/trunk/mynewsite/' else: PROJECT_ROOT = '/Users/paul/Sites/example.com/trunk/mynewsite/'
|
|
|

|
In settings.py, uncomment and set the ADMINS variable so that it includes your name and e-mail address. This will allow you to receive error reports.
|
|
|

|
You can deploy to a variety of web servers using a variety of modules (which create the necessary http - python bridge). In our case, we will concentrate on Apache and mod_wsgi
|
|
|
|

|
Create a new Apache configuration file under /etc/apache2/sites-available/
|
|
|

|
TODO link to example sites-available apache configuration file
|
|
|

|
Create a new WSGI configuration file: /var/www/<DOMAIN>/trunk/<MYPROJECT>/apache/django.wsgi
|
|
|

|
TODO link to example django.wsgi file
|
|
|

|
Run "a2ensite <DOMAIN-NAME>"
|
|
|

|
Run "/etc/init.d/apache2 reload"
|
|