Introduction:
Django is the modern base stack for web development. Compared with Ruby on Rails, I think I like Django better:
- Build-in ready to use admin interface
- Written in Python, with better performance
- Simpler and reasonable project struct
- Smaller base stack, but you can add what you want (ajax, south, REST, oauth …)
Essential tools for Django development:
virtualenv
We use this to manage the library for the project. You can have different environments (different python modules, different python versions) for every projects.
Create new clean environment foobar
virtualenv –no-site-packages foobar
Active the environment foobar
source foobar/bin/activate
Then install library in the environment foobar
pip …
At the end leave the environment foobar
deactivate
pip or easy_install
To install python libraries.
Django south
Database migration tool. Alertanative tool for syncdb
Create migration for your app
./manage.py schemamigration myapp –initial
Apply the migration
./manage.py migrate myapp
Then change the model myapp, and make a new migratation
./manage.py schemamigration myapp –auto
And apply it
./manage.py migrate myapp django-annoying
Useful decorators:
rederto ajaxrequest JSONField getobjectorNone
django-compressor
Compresses linked and inline javascript or CSS into a single cached file. And support coffeescript and less css framework.
django-pagination
pagination tools
django-piston
A mini-framework for Django for creating RESTful APIs.
django-celery
Celery integration for Django.
Celery is a task queue/job queue based on distributed message passing.
django-mongodb
MongoDB backend
django-admin-tools
Custom user interface, customizable dashboard
django-socialauth
oauth for social sites, login with twitter account, facebook account or gmail account
django-storages
Amazon S3, MogileFS file storage
django-grappelli
A jazzy skin for the Django Admin-Interface
django-profiles
A simple application which provides basic features for working with custom user profiles in Django projects.
django-registration
User registration and login
simplejson
JSON encoding and decoding
django-imagekit
Image process, support S3.
Beautiful Soup
Web Scrapping tool
django-workflows
django-workflows provides a generic workflow engine for Django.
The Django development environment steps:
How to install the virtualenv & django:
sudo apt-get install python-virtualenv
virtualenv –no-site-packages env_nm
source env_nm/bin/activate
pip install django
… ( install other modules )
deactivate
Export the libraries of the environment to a configuration file
pip freeze > requirements.txt
Install the environment in a clean environment
pip install -r requirements.txt
The missing part of Django settings.py
import os
ROOT_PATH = os.path.dirname(file)
MEDIAROOT = os.path.join(ROOTPATH, ‘static’)
TEMPLATE_DIRS = (
os.path.join(ROOT_PATH, ‘templates’)
)
Django Signals – You should pay attentation to this
This is the ECA (Aka: Event Condition Action) part of Django.
ECA is the essentail part of modern web appliactions.
Other useful modules:
SQLAlchemy – use this when the django ORM is not enough.
Haystack – Modular search for Django.
Django-mptt – Preorder tree management