- Published on
Django 学习笔记
- Authors
- Name
- Lucas Xu
- @xianminx
April 15, 2014
Writing your first Django app, part 1, Template
Django follows MVC architecture pattern
Install
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
settings.py
DB
debug
Applications
- Project v.s. Apps
- Create App
$ python manage.py startapp polls
April 16, 2014
Writing your first Django app, part 2, Model
- start server
python manage.py runserver
- admin site
http://127.0.0.1/admin
- Make the poll app modifiable in the admin
- Customize the admin form
reorder fields
add field title
add html classes
add related objects, StackedInline, TabularInline
- Customize the admin change list
list_display
admin_order_field
short_description
list_filter
search_fields
change list pagination
- 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
- Django views are web pages. URL pattern, urlconfs: map url patterns to views.
- Write your first view url() arguments, regex, view, kwargs, name
- 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
{% raw %} {% %} {% endraw %}
- Namespacing URL names
April 16, 2014
Writing your first Django app, part 4, Form
- Write a simple form
- Use generic views: Less code is better
April 16, 2014
Writing your first Django app, part 5, Testing
- automated testing
- basic testing strategy
- first test
- test a view
April 16, 2014
Writing your first Django app, part 6, static files
- Customize your app’s look and feel
- Adding a background-image
April 22, 2014
Advanced tutorial: How to write reusable apps
- package django apps to a tar file
- virtualenv
- python package index PyPI
May 14, 2014
i18n, l10n
Logging
May 15, 2014
Python 包管理
大部分现代语言都需要有包管理的机制,使得开发的组件可以重用。
Java使用jar打包组件,使用maven来管理组件和组件之间的依赖。
Python使用 pip
来安装和管理本地的组件。 其他工具有 easy_install
, distribute
pip
从 PyPI
(Python Package Index)线上网站查找需要的包,如同maven从mvncentral查找jar包。
Python使用 Eggs
,如同Java使用 jar
。
WSGI
Web Server Gateway interface
Werkzeug
Werkzeug is a WSGI utility for Python
Q
- Run server by
python manage.py runserver
. How to start it as background service?