第1次 时间:2013/11/10 12:39:05
1、install:
pip install Django==1.6
or
Download Django-1.6.tar.gz
tar -zxvf Django-1.6.tar.gz
cd Django-1.6
sudo python setup.py install
or
git clone https://github.com/django/django.git
2、Create Project:
django-admin.py startproject mysite
3、Python 連接 MySQL
MySQL 是十分流行的開源資料庫系統,很多網站也是使用 MySQL 作為後台資料儲存,而 Python 要連接 MySQL 可以使用 MySQL 模組。MySQLdb 模組可以讓 Python 程式連線到 MySQL server, 執行 SQL 語句及擷取資料等。
開始前要確定系統內的 Python 有安裝 MySQLdb 模式,你可以 Python command line interpreter 檢查,在指令模式輸入 python,然後便可以開始檢查:
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named MySQLdb
>>> exit()
如果見以上面的 "ImportError: No module named MySQLdb" 一句,便表示系統沒有安裝,到 MySQLdb 官方網站 下載 MySQLdb,並用以下方法安裝:
$ tar zxvf MySQL-python-1.2.2.tar.gz
$ cd MySQL-python-1.2.2
$ python setup.py build
$ python setup.py install
安裝好 MySQLdb 後便可以編寫程式碼,以下是簡單的例子:
PLAIN TEXT
CODE:
#!/usr/bin/python
# 引入 MySQL 模組
import MySQLdb
# 連接到 MySQL
db = MySQLdb.connect(host="localhost", user="db_user", passwd="db_pass", db="db_name")
cursor = db.cursor()
# 執行 SQL 語句
cursor.execute("SELECT * FROM db_table")
result = cursor.fetchall()
# 輸出結果
for record in result:
print record[0]
4、python manage.py syncdb (数据库)
5、python manage.py startapp [appnames]
6、python manage.py sql polls
7、python manage.py validate
python manage.py sqlcustom polls
python manage.py sqlclear polls
python manage.py sqlindexes polls
python manage.py sqlall polls
8、pyhthon manage.py syncdb
9、python manage.py shell
10、http://127.0.0.1:8000/admin/
11、# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^testproject/', include('testproject.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
Settings.py:
...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'polls'
)
...
12、admin.py:
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question']
admin.site.register(Poll, PollAdmin)
13、admin.py:
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin):
fieldsets = [(None,{'fields':['question']}),('Date information', {'fields':['pub_date']}),]
admin.site.register(Poll, PollAdmin)
14、admin.py:
from django.contrib import admin
from polls.models import Poll
class PollAdmin(admin.ModelAdmin):
fieldsets = [(None,{'fields':['question']}),('Date information', {'fields':['pub_date'],'classes':['collapse']}), ]
admin.site.register(Poll, PollAdmin)
15、
第2次 时间:待续