One of the most important things I've done to improve my experience working with Django is to develop a consistent pipeline between development and deployment. Here I'll quickly outline some of the things I've done to make the experience easier, but I'm curious to hear about the techniques that you use as well.
local_settings.py + +
One of the best tricks for working with Django deployment is to end your
Doing that lets you override
I create a
Production_settings.py.file with settings for production servers, and a
devel_settings.py.for local development. I add both of them to version control, and then creating a
本地设置。py我的地方结帐that looks like this:
Finally, I make sure that
本地设置。pytrick) makes it easy to modify shared settings, but in addition this trick also makes it easy to modify the production settings withing SSHing into the production server, and is simpler than maintaining a production and development branch in the repository (which requires a lot of pointless merging).
devel_settings.py.file almost always looks exactly the same regardless of the project it is part of. Yours may look a bit different, but I find that it saves a few precious moments of thinking to just keep a standardized project template somewhere (instead of using
djangoadmin startprojectto create new projects).
Mine looks like this:
# Settings for devel server进口OS.ROOT_PATH=OS.。path。dirname(__file__)调试=TrueTEMPLATE_DEBUG=调试COMPRESS=False# django-compress settingCACHE_BACKEND="locmem:///"DATABASE_ENGINE='sqlite3'数据库名称=OS.。path。join(ROOT_PATH,'devel.sqlite')DATABASE_USER=''DATABASE_PASSWORD=''database_host.=''database_port.=''MEDIA_ROOT=OS.。path。join(ROOT_PATH,'media')MEDIA_URL='http://127.0.0.1:8000/media/'admin_media_prefix.='/media/admin/'
# Django settings for codernode.com projectCOMPRESS=True# django-compress settingcompress_version.=True# django-compress setting调试=TrueTEMPLATE_DEBUG=调试管理员=(('Mr Admin','email@example.com'),)MANAGERS=管理员CACHE_BACKEND="memcached://127.0.0.1:11211"电子邮件_subject_prefix.="[My Project]"server_email.="firstname.lastname@example.org"DEFAULT_FROM_EMAILemail@example.com'DATABASE_ENGINE='postgresql_psycopg2'数据库名称='some_database'DATABASE_USER='some_user'DATABASE_PASSWORD='some_password'database_host.=''database_port.=''时区='America/Chicago'LANGUAGE_CODE='en-US'site_id.=1USE_I18N=TrueMEDIA_ROOT='/ path / to / media / root /'MEDIA_URL='http://example.com/media/'admin_media_prefix.='/media/admin/'TEMPLATE_DIRS=()
Yours will likely look quite different.
A local repository for each developer, which they push out to the master server at regular intervals.
Developers do most of their work on their local machines, and push it to an appropriate branch on the master repository. They also pull from the master server occasionally to keep the repositories synchronized.
The production repositories are kept up to date by either:
UsingFabricto command them to pull the newest changes. This is the best option for large, medium and small deployments. That is, this is always the best option. Don't bother reading the others.
Setup a cronjob that pulls the changes periodically. This is the simplest and laziest of the approaches, but is a bad idea in most circumstances.
Using this setup, pushing a change on the development server to the production servers is lovable two steps:
fab revert hash-to-revert-to当您的部署并不相当不合适时，为那些尴尬的时刻令人尴尬的时刻。
Production Server Setup
I keep my directory layouts very simple and uniform. All production servers have a
djangouser whose password has been disabled and doesn't have access to SSH. The developers have accounts on the production machines, and they are members of the
I organize libraries into folders based on the version control system they use, so I'll typically have a
Git.folder and a
svnfolder, but I might have an
bzrfolder, but I wanted to be inclusive.) Then I symlink them all into the
/usr/python2.?/site-packages/folder (I've been using Python 2.5 pretty exclusively of late, but I imagine in the next year or two I may move up to 2.6 if there are any compelling performance gains).
setup.pyto install the libraries), since it makes it easier to update the libraries across all production servers at a later point if necessary.
/var/www/example.com/media/, and let Nginx handle serving it. (I'm still quite happy with my Nginx/Apache2/mod_python stack, even if it has fallen out of favor with the coolest kids for WSGI.)
There are a lot of creative things you can do with integrating testing into the mix (deploy script only pushes changes to production if all the tests pass, or using a post-commit hook for the master repository to run the test battery and email developers if any tests fail, etc), but I'm still at a stage where I run tests manually. This will probably be the next area that I start improving upon my setup.