Some notes on handling Development and Production Servers with Django.
How to find out if we are on development or production?
The first thing I do is to define a variable “DEVELOPMENT_MODE“ in settings.py, that checks if the server is running on my local machine or not. I found this nice idea on the djangoproject.com website: http://code.djangoproject.com/browser/djangoproject.com/django_website/settings.py
import os, platform DEVELOPMENT_MODE = (platform.node() != "my_hostname")
Set the hostname, you can find it out with the command
hostname
in your terminal.
Now on your local machine DEVELOPMENT_MODE variable is True, on production should be False.
settings
With this variable you can split your settings for production and development. This makes sense for the settings like DATABASE_*, DEBUG, MEDIA_*, CACHE_BACKEND and others none django-related-stuff like google-API keys.
An example of my settings.py:
if DEVELOPMENT_MODE:
DEBUG = True
DATABASE_ENGINE = 'postgresql_psycopg2'
DATABASE_NAME = 'xxx'
DATABASE_USER = 'xxx'
DATABASE_PASSWORD = 'xxx'
DATABASE_HOST = 'localhost'
TEMPLATE_DIRS = ('/xxx/templates')
MEDIA_ROOT = '/xxx/media/'
MEDIA_URL = '/media/'
GOOGLE_KEY = "ABQIAAAA2OXrNyQ" #localhost
CACHE_BACKEND = 'dummy:///'
else:
DEBUG = False
DATABASE_ENGINE = 'postgresql_psycopg2'
DATABASE_NAME = 'xxx'
DATABASE_USER = 'xxx'
DATABASE_PASSWORD = 'xxx'
DATABASE_HOST = ''
TEMPLATE_DIRS = ('/xxx/templates',)
MEDIA_ROOT = '/var/www/server/media/'
MEDIA_URL = 'http://server/media/'
GOOGLE_KEY = "ABQIA66g" #domain
CACHE_BACKEND = 'file:///var/tmp/django_cache'
DATABASE_PORT = ''
...
serve static files
I had a long time on my local machine an apache2 for serving static/media files running. I changed this setting now to ‘django.views.static.serve’ which is the build-in fileserver from django. The reason was that I didn’t want to configure on every webdesigners machine the apache2, only for serving the media. ![]()
With the DEVELOPMENT_MODE variable it’s easy to do that. Only append this to the urls.py.
from settings import DEVELOPMENT_MODE
if DEVELOPMENT_MODE:
urlpatterns += patterns('',
url(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': "/media/"}),
)
The ‘django.views.static.serve’ works very well for development, but never ever use it on production!
http://www.djangoproject.com/documentation/static_files/
caching
When you use a cache framework, I’m sure you want it only on your production site, so you can use on your local machine the dummy cache that doesn’t actually cache.
if DEVELOPMENT_MODE:
CACHE_BACKEND = 'dummy:///'
...
http://www.djangoproject.com/documentation/cache/#dummy-caching-for-development
database
I’m using PostgreSQL now on development and production site. I found it hard to import/export dumps (structure/data) from Sqlite to PostgreSQL and vice versa. I worked a lot with MySQL and have the most experience with it, but with Django it really sucks sometimes, no fixtures support and no transactions.
Ok that’s it I hope you enjoy reading and please give me some of your tips.
No comments yet
Leave a reply