chapter 3
django工程的布局
<repository_root>/
<django_project_root>/
<configuration_root>/
Top level:Repository Root
最上层是工程根目录的绝对路径
这里还经常有的文件是:
README
doc/
design/
.gitignore
requirement.txt
Second level:Django project root
这里通常是由django-admin.py startproject 命令产生的
这里还包括:
configuration_root
media/
static/
templates/
各种apps 的文件夹
Third level:configuration root
也是由 django-admin.py startproject 产生的
包括:
urls.py
settings.py等
简单的布局类似是这样:
icratings_project/
.gitignore
Makefile
docs/
requirements.txt
icratings/
manage.py
media/
products/
profiles/
ratings/
static/
templates/
icratings/
__init__.py
settings/
urls.py
wsgi.py
不需要把 virtualenv的内容放到版本控制中去,只要把需要的信息放到requirement.txt中就行了
chapter 4
django基本的app设计app设计的黄金法则:
Write programs that do one thing and do it well.
功能专一并且松耦合
一个工程中的实用app案例:
我们建立一个商店,使用twoscoops_project这个工程目录
这个Django project has:
•A flavors app to track all of our ice cream %avors and list them on our website.•A blog app for the o fficial Two Scoops blog.
•An events app to display listings of our shop's events on our website:events such as Strawberry Sundae Sundays and Fudgy First Fridays.
在将来可能增加的功能:
*A shop app to allow us to sell pints by mail order.
*A tickets app, which would handle ticket sales for premium all-you-can-eat icecream fests.
由于tickets这个功能很多应用可能不会用到,而且会有比较复杂的逻辑,所以我们单独分出一个app
怎么给app命名呢?
*当个单词,容易理解和维护 animals , blog, polls等
*有时还要考虑url的因素,适当调节名称
保持app比较小巧
chapter 5
配置和所需的文件好的实践经验:
*所有的配置文件需要版本控制
*DRY原则,相同配置继承即可
开发环境的配置不要在版本控制中,本地需要配置的debug等和生产环境区别很大。
使用多配置请参看 http://www.slideshare.net/jacobian/the-best-and-worst-of-django
建立settings模块来处理不同配置的使用
settings/
__init__.py
base.py
local.py
staging.py
test.py
production.py
使用时候可以这样使用:
$ django-admin.py runserver --settings=twoscoops.settings.local
一个开发环境的配置案例:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'twoscoops',
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}
INSTALLED_APPS += ('debug_toolbar', )
INTERNAL_IPS = ('127.0.0.1',)
MIDDLEWARE_CLASSES += \
('debug_toolbar.middleware.DebugToolbarMiddleware', )
使用方法:$ django-admin.py runserver --settings=twoscoops.settings.local
使用环境变量来保存密码
requirement也是用类似的模块处理不同环境下不同开发人员的不同配置