1、下載需要模塊。
a、apache (httpd-2.0.54.tar.gz or later)
b、mod_python (mod_python-3.3.1.tgz)
c、Django (Django-0.96.tar.gz)
默認已安裝 Python (http://www.python.org)
2、安裝配置環(huán)境。
首先,正常安裝 apache 和 mod_python (http://www.modpython.org/),注意要打開 apache 的 DSO 功能,執(zhí)行:
#...
#tar -zxvf httpd-2.0.54.tar.gz
#cd httpd-2.0.54
#./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=all
#make install clean
#...
#tar -zxvf mod_python-3.3.1.tgz
#cd mod_python-3.3.1
#./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/bin/python
#make install clean
#...
#tar -zxvf Django-0.96.tar.gz
#cd Django-0.96
#python setup.py install (If
you installed Django using setup.py install, uninstalling is as simple
as deleting the django directory from your Python site-packages.)
#...
這
樣我們就安裝好 Apache + mod_python + Django 了,你可以執(zhí)行 apachectl start
測試一下能不能成功。然后我們開始配置環(huán)境,首先配置 httpd.conf 加入 LoadModule python_module
modules/mod_python.so (在安裝 mod_python 的時候安裝程序會自動把 mod_python.so 拷貝到
apache 的 modules 目錄下),接著配置虛擬主機。
到這里你就可以利用 mod_python
來進行編程了,然后我們來配置一下 Django 并通過一個實例來讓大家對這個現(xiàn)今最 HOT 的 python web
框架有一個大體的了解:首先,我們來學習一下用 django-admin.py 工具來簡化你的工作 (當我們安裝 Django
的時候,安裝程序會自動把 django-admin.py 拷貝到 系統(tǒng) PATH 下,所以我們可以直接使用它)。首先進入到我們的 python
程序目錄 (我用的是:{DOCUMENT_ROOT}/python),執(zhí)行:
#django-admin.py startproject newtest
這樣就可以生成我們的測試工程了默認情況下會生成 {DOCUMENT_ROOT}/python/newtest 目錄,該目錄下會有如下文件:
__init__.py (表示這是一個 Python 的包)
manage.py (提供簡單化的 django-admin.py 命令,特別是可以自動進行 DJANGO_SETTINGS_MODULES 和 PYTHONPATH 的處理,而沒有這個命令,處理上面環(huán)境變量是件麻煩的事情)
settings.py (它是django的配置文件)
uls.py (url映射處理文件, Karrigell 沒有這種機制,它通過目錄/文件/方法來自動對應,而 Django 的url映射是url對于某個模塊方法的映射,目前不能自動完成)
然后我們在 apache 的虛擬主機配置文件里面加上:
<Location "/newtest/">
SetHandler python-program
PythonPath "sys.path+['{DOCUMENT_ROOT}/python']"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE newtest.settings
#PythonInterpreter mysite
PythonDebug On
</Location>
這
里要注意的是對于 PythonPath,必須設置成工程目錄 ({DOCUMENT_ROOT}/python/newtest)
的上一級目錄!這樣我們就完成了 Django 和 apache 的整合了,趕快試一下吧,我們寫一個 action 來測試一下 Django
的功能:
3、編寫測試程序。
1> 首先,創(chuàng)建 ./list.py :
#coding=utf-8
from django.shortcuts import render_to_response
address = [
{'name':'張三', 'address':'地址一'},
{'name':'李四', 'address':'地址二'}
]
def index(request):
return render_to_response('list.html', {'address': address})
2> 然后,創(chuàng)建一個模版文件 ./templates/list.html :
<h2>通訊錄</h2>
<table border="1">
<tr><th>姓名</th><th>地址</th></tr>
{% for user in address %}
<tr>
<td>{{ user.name }}</td>
<td>{{ user.address }}</td>
</tr>
{% endfor %}
</table>
3> 設置模版目錄 (編輯 ./settings.py) :
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
'./templates',
)
4> 修改 urls.py :
from django.conf.urls.defaults import *
urlpatterns = patterns('',
# Example:
# (r'^newtest/', include('newtest.foo.urls')),
(r'^newtest/list/$', 'newtest.list.index'),
# Uncomment this for admin:
# (r'^admin/', include('django.contrib.admin.urls')),
)
5> 重啟 Apache 并訪問相應 url (http://localhost/newtest/list/) 即可看到結果頁面了:
通訊錄
姓名 地址
張三 地址一
李四 地址二
到這里,你已經(jīng)掌握了 Django 框架的基本內容了,就可以進一步學習 Django 的其他內容了,Enjoy It :)