Django 学习笔记

April 15, 2014

Writing your first Django app, part 1, Template

  1. Django follows MVC architecture pattern
  2. Install
  3. Hello, World
    • create project
       django-admin.py startproject mysite
    
    • project structure

      ➜  python  tree mysite
      mysite
      ├── manage.py
      └── mysite
          ├── __init__.py
          ├── settings.py
          ├── urls.py
          └── wsgi.py
      
    • Run

  4. settings.py
    • DB
    • debug
    • Applications
  5. Project v.s. Apps
  6. Create App
     $ python manage.py startapp polls
    

April 16, 2014

Writing your first Django app, part 2, Model

  1. start server python manage.py runserver
  2. admin site http://127.0.0.1/admin
  3. Make the poll app modifiable in the admin
  4. Customize the admin form
    • reorder fields
    • add field title
    • add html classes
    • add related objects, StackedInline, TabularInline
  5. Customize the admin change list
    • list_display
    • admin_order_field
    • short_description
    • list_filter
    • search_fields
    • change list pagination
  6. Customize the admin look and feel
    • Customize your project’s templates
    • django template file
    • customize the admin index page

April 16, 2014

Writing your first Django app, part 3, View

  1. Django views are web pages. URL pattern, urlconfs: map url patterns to views.
  2. Write your first view url() arguments, regex, view, kwargs, name
  3. write views that actually do something
    • return HttpResponse
    • Raise exception such as Http404
    • Django db API
    • Template System
    • loader
    • render
    • removing hardcoded urls in template by using {% %}
  4. Namespacing URL names

April 16, 2014

Writing your first Django app, part 4, Form

  1. Write a simple form
  2. Use generic views: Less code is better

April 16, 2014

Writing your first Django app, part 5, Testing

  1. automated testing
  2. basic testing strategy
  3. first test
  4. test a view

April 16, 2014

Writing your first Django app, part 6, static files

  1. Customize your app’s look and feel
  2. Adding a background-image

April 22, 2014

Advanced tutorial: How to write reusable apps

  1. package django apps to a tar file
  2. virtualenv
  3. python package index PyPI

May 14, 2014

i18n, l10n

Logging

May 15, 2014

Python 包管理

大部分现代语言都需要有包管理的机制, 使得开发的组件可以重用。

Java使用jar 打包组件, 使用maven来管理组件和组件之间的依赖。

Python 使用pip来安装和管理本地的组件。 其他工具有easy_install, distribute

pipPyPI (Python Package Index) 线上网站查找需要的包,如同maven 从mvncentral 查找jar包。

Python 使用Eggs, 如同Java 使用jar

WSGI

Web Server Gateway interface

Werkzeug

Werkzeug is a WSGI utility for Python

Q

  1. Run server by python manage.py runserver. How to start it as background service?