Ben Emson

How To Set Up and Build a Django Web Site

This article outlines how to set up your new Django site from scratch. I tend to use Dreamhost Web Site Hosting so there will be a few notes specific to them.

If you are unsure how to set up Python and Django using Dreamhost then checkout my previous article.

I hope you find it useful and drop me a comment if you have any questions.

Create the new site

Create a project directory Liquid error: No such file or directory - posix_spawnp

Create you new site: Liquid error: No such file or directory - posix_spawnp

Now create the following directories: Liquid error: No such file or directory - posix_spawnp

Initialise git

Liquid error: No such file or directory - posix_spawnp

Add the following:

.DS_Store
*.pyc
packages/
work/

Add WSGI file

The following should be carried out from this directory: Liquid error: No such file or directory - posix_spawnp

Add the passenger_wsgi.py file to mysite-project/mysite.com directory:

Liquid error: No such file or directory - posix_spawnp Create the public/appmedia directory in this directory to. Create tmp/restart.txt

Set up the VirtualEnv Environment

Move to the mysite-project directory. Ensure that you have virtualenv installed on your local system: Liquid error: No such file or directory - posix_spawnp

Create the virtual/ directories using virtualenv: Liquid error: No such file or directory - posix_spawnp

Create a symbolic link to the activate executable. Liquid error: No such file or directory - posix_spawnp

NB: if you rename your mysite-project directory you may well need to delete the virtual directory and activate symbolic link and recreate them using the previous virtualenv steps.

You can check which Python site packages being used with this command: Liquid error: No such file or directory - posix_spawnp

Create Local Dev Database

Change directory to the scripts directory in mysite-project/mysite.com/mysite: Liquid error: No such file or directory - posix_spawnp

create-db.sql Liquid error: No such file or directory - posix_spawnp

drop-db.sql Liquid error: No such file or directory - posix_spawnp

Now create the database Liquid error: No such file or directory - posix_spawnp

Set up the Django Project Files

Make the following changes to the settings.py file. Liquid error: No such file or directory - posix_spawnp

Now we need to create a settings_local.py file in the same directory as the main settings.py file: Liquid error: No such file or directory - posix_spawnp

Then add your default settings (most probably your dev settings): Liquid error: No such file or directory - posix_spawnp

As we will be using multiple environments we need to have different configurations for each environment. There are a number of ways to do this, however the DRYist and easiest is to have a main settings file that imports different settings according to the environment.

Change directory to the mysite-project/settings directory, and create the appropriate settings files - we will have a dev, staging and production file as well as the main one: Liquid error: No such file or directory - posix_spawnp

Now add the appropriate settings to these files database settings. Note that these settings will overwrite the settings in the main settings file.

Adding South to Your Application

For detailed installation instructions see http://south.aeracode.org/wiki/Download.

Note, for MySQL databases, you should install South to a new instance of your MySQL database and not an existing or legacy database. This is because South won't update it's self when you run: python manage.py migrate

Quick installation.

Add South (Note the capital 'S') to your pip requirements.txt file, and pip install it: Liquid error: No such file or directory - posix_spawnp

Or just run: Liquid error: No such file or directory - posix_spawnp

Install the south (Note lowercase 's') into your project settings.py file. e.g.: Liquid error: No such file or directory - posix_spawnp

Now syncdb with your database so that your App knows about South. Liquid error: No such file or directory - posix_spawnp

You should get an output similar to this:

Syncing...
Creating table south_migrationhistory

Synced:
 > django.contrib.admin
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.markup
 > south
 > tagging

Not synced (use migrations):
 - 
(use ./manage.py migrate to migrate these)

Finally you need to tell South what Apps you want it to manage (in terms of migrations). So for example say you have an App called 'firepages', then run: Liquid error: No such file or directory - posix_spawnp

This will create a migrations directory and will allow South to manage all the models in that App:

  Creating migrations directory at '/mypath/mysite-project/mysite.com/mysite/firepages/migrations'...
  Creating __init__.py in '/mypath/mysite-project/mysite.com/mysite/firepages/migrations'...
   + Added model 'firepages.Group'
   + Added model 'firepages.Category'
   + Added model 'firepages.Page'
  Created 0001_initial.py.

Note that because you will be uploading this new migrations directory you shouldn't need to run this command on the server.

You will now need to execute the following command to migrate forward: Liquid error: No such file or directory - posix_spawnp

Now if you want to change your database do the following:

  1. Modify your model. e.g. add / remove fields
  2. Tell South to create a migration: python manage.py startmigration myapp name-of-my-migration --auto
  3. Execute the migration: python manage.py migrate

Change urls.py

Use the following imports: Liquid error: No such file or directory - posix_spawnp

At the bottom add a new URL path so that the site can get access to the appmedia, NB don't forget to add a staging and production version to: Liquid error: No such file or directory - posix_spawnp

Now add the following URLs: Liquid error: No such file or directory - posix_spawnp

You may want to change the admin URL pattern to be something else. This will make the admin area more obscure thus helping to prevent hacking. e.g. mylogin

Add a views.py file

In the mysite-production/mysite.com/mysite/ directory add a views.py file with the following: Liquid error: No such file or directory - posix_spawnp

Sync the DB and Start the Server

Navigate to the mysite-project/mysite.com/mysite directory

Start the virtual environment: Liquid error: No such file or directory - posix_spawnp

Now open a browser and point it at this URL: Liquid error: No such file or directory - posix_spawnp

It should display your hello view results.

Requirements

Add a requirements.txt file to the following directory: Liquid error: No such file or directory - posix_spawnp

Add in any requirements e.g. django-tagging Liquid error: No such file or directory - posix_spawnp

Execute and add requirements: Liquid error: No such file or directory - posix_spawnp

Local Apps Directory

Create an apps directory in the root of your project folder. Then tell your settings.py file about it:

  sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))

Now any apps that you want in here and not in your "pip install" directory just add them and they will be found.

RequestContext on All Views

All your views should implement a RequestContext variable, but first you will need to add the following CONTEXT_PROCESSORS to the settings.py file: Liquid error: No such file or directory - posix_spawnp

Now Make sure that you pass the RequestContext to the render. Liquid error: No such file or directory - posix_spawnp

This will allow you to use generic templates, and also get a handle on certain variables in the template.

Page Specific Menus

See this article for more information: http://gnuvince.wordpress.com/2007/09/14/a-django-template-tag-for-the-current-active-page/

Django doesn't out of the box provide a current_page variable like Ruby on Rails - but it is easy enough to implement.

You will need to have the 'django.core.context_processors.request', setting set in the template context processors as specified above.

You will also need to create a module called templatetags with a init.py file in it. Now add a file called active_menus.py Liquid error: No such file or directory - posix_spawnp

Now in your template load this template tag and call it like this: Liquid error: No such file or directory - posix_spawnp

Template Includes

When creating an include template file give it a name starting with an underscore e.g. _nav.html

Add a Sitemap to Your Site

Add the following to your settings.py file in the INSTALLED_APPS section:

    django.contrib.sitemaps

Now check that you have the following installed also.

This should be in the TEMPLATE_LOADERS:

  'django.template.loaders.app_directories.load_template_source',

Make sure you’ve installed the sites framework. Also make sure that you have set the site domain name and description correctly in the sites project in the Admin Panel. It should not be example.com, but something like: myproject.com

Create a sitemaps.py file in your project. This file should contain something like:

Liquid error: No such file or directory - posix_spawnp

Now add the following to your urls.py file: Liquid error: No such file or directory - posix_spawnp

Adding a new App

Liquid error: No such file or directory - posix_spawnp

Add myappname to your settings.py file INSTALLED_APPS Now create some models in your myappname/models.py file.

Then run a syncdb: Liquid error: No such file or directory - posix_spawnp

Now if you want to add South migrations: Liquid error: No such file or directory - posix_spawnp

Installing PIL

PIL should not be installed as part of the requirements.txt file, as you will need to compile libjpeg first.

blog comments powered by Disqus