Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          鑒于網上關于 Python 框架 Django 的 Apache + mod_python + Django 環境詳細安裝加實例文檔還是比較難找到,所以石頭在這邊寫一個文檔與初學 Django 的同志們以共勉:)

          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、安裝配置環境。

          首先,正常安裝 apache 和 mod_python (http://www.modpython.org/),注意要打開 apache 的 DSO 功能,執行:

          #...
          #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 了,你可以執行 apachectl start 測試一下能不能成功。然后我們開始配置環境,首先配置 httpd.conf 加入 LoadModule python_module modules/mod_python.so (在安裝 mod_python 的時候安裝程序會自動把 mod_python.so 拷貝到 apache 的 modules 目錄下),接著配置虛擬主機。

          到這里你就可以利用 mod_python 來進行編程了,然后我們來配置一下 Django 并通過一個實例來讓大家對這個現今最 HOT 的 python web 框架有一個大體的了解:首先,我們來學習一下用 django-admin.py 工具來簡化你的工作 (當我們安裝 Django 的時候,安裝程序會自動把 django-admin.py 拷貝到 系統 PATH 下,所以我們可以直接使用它)。首先進入到我們的 python 程序目錄 (我用的是:{DOCUMENT_ROOT}/python),執行:

          #django-admin.py startproject newtest

          這樣就可以生成我們的測試工程了默認情況下會生成 {DOCUMENT_ROOT}/python/newtest 目錄,該目錄下會有如下文件:

          __init__.py (表示這是一個 Python 的包)
          manage.py (提供簡單化的 django-admin.py 命令,特別是可以自動進行 DJANGO_SETTINGS_MODULES 和 PYTHONPATH 的處理,而沒有這個命令,處理上面環境變量是件麻煩的事情)
          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> 首先,創建 ./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> 然后,創建一個模版文件 ./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/) 即可看到結果頁面了:

          通訊錄
          姓名 地址
          張三 地址一
          李四 地址二

          到這里,你已經掌握了 Django 框架的基本內容了,就可以進一步學習 Django 的其他內容了,Enjoy It :)

          posted on 2008-11-06 20:57 seal 閱讀(1119) 評論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 景东| 西和县| 南郑县| 应城市| 沙洋县| 浏阳市| 明水县| 呼图壁县| 新竹县| 全南县| 东兰县| 扶余县| 凭祥市| 屯留县| 天水市| 木兰县| 彭泽县| 汤阴县| 迭部县| 盐源县| 马山县| 永川市| 鹤峰县| 华池县| 赞皇县| 灵台县| 洛阳市| 扎赉特旗| 海兴县| 安康市| 慈利县| 松溪县| 深州市| 吉水县| 武安市| 庆安县| 蓬莱市| 江安县| 蓝田县| 迁西县| 高清|