Category: Programming

Django

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

Recently. I totally changed my development tools at home. I can not change that at the office since that will also be lots of .NET development and the project related to M$ OFFICE SDK.

Developers always interested to share or show off there development tools, because they want to improve there productivity.

You can see how developers care about there development machine and tools:


What is my development tools and software?

I use Ubuntu 11.04 as the main OS. There will be tons of work to do after you install the default Ubuntu.

I installed more fonts such as MSYH, Arial, then you will see the similar web page when you surfing the web.

Change the Ubuntu theme back to Ubuntu 10 style.

Soft wares installed under Ubuntu:

1. JDK (power lots of tools, support JAVA development)

2. Chromium (Main browser)

3. GIMP (Photoshop under Linux)

4. Dropbox (Sync tool for my documents)

5. Filezilla (FTP client)

6. Skype (Chatting tool)

7. IDE:

Aptana (Support PHP, Python, Ruby developement. But not used very frequently)

Eclipse (Java development)

Gvim (My main development tool)

8. Ubuntu tweak (configuration tool)

9. Chromium plugins:

Google reader notifier

Screen capture

Color pick

10. Vim plugins:

Vim wiki

Calander

ctags

Nerdtree

bufferexplorer

taglist

zencoding

11. Avant window navigator (Similar as Dock in Mac OS)

12. Mysql, PHP, Apache2, Nodejs, mongodb, git, svn, redis and lots of shell scripts to complete tasks.

The nodejs growing very fast recently. New library or framework added to nodejs  almost everyday, since it is very easy to build a nodejs based module and javascript is used very widely.

I have done the A/B testing of nodeJS vs Erlang mochiweb, Erlang misultin, Nginx static page on my poor VM machine, and got the following result:

Nodejs helloworld:                     724 qps

Erlang mochiweb helloworld:     430 qps

Erlang misultin helloworld:         690 qps

Nginx static page:                    1045 qps

You can see that nodeJS is bloody fast thanks to the good performance of V8 JavaScript engine.

Talk about server side javascript, you should say Rhino, but nodejs is 20x faster than Rhino. NodeJS is stable, and not seen a bug of the code, like Nginx. You can build asynchronous system based on nodeJS and drop Tornado. What is important is that you can reuse your code both on client side and server side.

NodeJS is good at heavy IO usage, but not heavy CPU usage. And you should not change your current web system to nodeJS since it is too young.You can build your JSON interface by NodeJS, construct JSON from data fetched from database or remote web service, output the JSON format result.

Nodejs library:

npm — A node package manager that uses CommonJS-compatible package.json files, written in asynchronous JavaScript.

Express — A robust feature rich web development framework inspired by Sinatra

nodemachine — A port of WebMachine to Node.js

Connect — A  middleware framework for node

Socket.io — WebSocket-compatible server and client with fallback for legacy browsers

node-mysql — A node.js module implementing the MySQL protocol

redis2json — Easily loads data from Redis into structured JS object

See more:

https://github.com/joyent/node/wiki/modules