Python 大神柳开闫教你用 Django!
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
import this
Simple, funny, powerful.
void 大神:
用 Django 好几年了,月月都舒服。
安装 Python、Django。
Python 已经默认安装,只需要先安装 pip(是什么?):
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
然后安装 Django:
pip install Django
以上。好简单!
下载 Python 并安装(注意要下载 2.x 的版本):
http://python.org/getit/
将 python.exe 加入环境变量 path。
然后下载 Django 并解压:
https://www.djangoproject.com/download/
最后在相应目录输入并运行:
python setup.py install
django-admin startproject yourname
cd yourname
django-admin startapp blog
在 settings.py 文件中将 DATABASES 设置为如下内容:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'blog/db/database.db',
}
}
INSTALLED_APPS = (
...
'django.contrib.admin',
'yourname.blog',
)
python manage.py runserver
- 标题
- 时间
- 内容
- ...
python manage.py syncdb
admin.site.register(YourModel)
Controller 用来处理 Model 中的数据,传递给 View。
Why need this?
例:1989年的文章列表
- Good URL:
http://yzheng.me/1989/
- Bad URL:
http://yzheng.me/article_list.php?year=1989
View 是网站的前端部分,从 Controller 获得数据,由很多模版组成。
模版就是常规的 HTML 文件。
{{ variables }}
{% for ele in list %}
do something w/ ele ...
{% endfor %}
- Category, tag?
- Comment?
- Beautiful template?
- ...
- With group, have a try!
- Django 文档: https://docs.djangoproject.com/
- Django Book: http://djangobook.py3k.cn/2.0/
- The Python Tutorial: http://docs.python.org/tutorial/