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

More and more cloud services enter our life. Such as Cloud file store service Dropbox. We can not only rely on the Safety of the service provider. 

Two way to encrypt your content or files:

1. Encrypt by password in VIM

Just enter:

:set key=<password>

when you editing the file, then the file will be encrypted.

When opening the encrypted file, you will be asked to input the password.

If you want to convert a encrypted file to be a plain file, you should set the <password> to be empty.

2. Encrypt files by mcrypt in UBUNTU:

Install mcrypt:

sudo apt-get install mycrypt

Encrypt a file with password:

mcrypt -a blowfish test.txt

You will get a file named 

test.txt.nc

Decrypt the .nc file

mycrypt -d test.txt.nc

 

CoffeeScript 是在JavaScript 基础上增加了很多易读的语法糖的新一代编程语言。

CoffeeScript 编译成 JavaScript 在客户端执行,或者在 NodeJS 服务器端执行。它的语法本质就是 JavaScript ,但是提高了代码的可读性。

在 JavaScript 基础上增加了很多 Ruby Python 的语法特性。比如,用缩进代替括号,取消了作为行结束的分号,简化function定义, 用 @ 符号替代OO编程中到处用到的 this 。

增加了很多编程的强制规范,比如默认变量为局部变量,函数最后一行为返回值。

提供了很多 JavaScript 编程经常用到的模式,比如可以给函数提供默认值,改进的循环处理。

由于 CoffeeScript 编译为 JavaScript ,所以可以在任何需要 JavaScript 的地方用 CoffeeScript 取代它。如果说 Web 开发的趋势是 HTML5 和 服务器端 JavaScript 。那么 Web 开发的趋势就是 CoffeeScript 。你可以只写一种优雅的现代语言 CoffeeScript 。

CoffeeScript 编写和开发环境

以 Ubuntu 为例

需要安装 NodeJS 作为编译器环境

安装 npm 包管理器

安装 coffee-script 编译器

npm install -g coffee-script

安装 VIM 插件 vim-coffee-script

然后在 VIM 中执行 

:CoffeeCompile watch vert

这样在右边窗口开发 CoffeeScript ,可以在左边窗口实时查看被编译成的 JavaScript
也可以用命令行 coffee -c test.coffee 来手动编译

常用的 CoffeeScript 语法例子

定义一个连续数组

numbers = [0..3]  

循环操作数列

sites = ['blog.eood.cn','www.eood.cn','google.com']
for site in sites
 alert site

甚至可以这样

sites = ['blog.eood.cn','www.eood.cn','google.com']
alert site for site in sites

定义函数

myfunc = (arg) ->
  alert arg

或者

myfunc:  ->
  alert 'ok'

定义复杂对象

kids =
  brother:
    name: "Max"
    age:  11
  sister:
    name: "Ida"
    age:  9

同过 JQuery 操作 DOM

$('.account').attr class: 'active‘

定义类和类的继承

class Animal
  constructor: (@name) ->

  move: (meters) ->
    alert @name + " moved #{meters}m."

class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5

class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45

绑定事件函数到 DOM 对象

  $('.shopping_cart').bind 'click', (event) =>
    @customer.purchase @cart

CoffeeScript 实际生产环境的使用

可以结合 JQuery, Backbone JS 或者所有的 JavaScript 开发框架来开发复杂的前端应用和服务器端应用。37signals 利用了 CoffeeScript + BackboneJS 开发了 HTML5 的手机客户端程序。用 CoffeeScript 来书写 JavaScript 可以减少代码量,不降低代码执行效率,降低开发成本。