本文實例講述了python+Django+apache的配置方法。分享給大家供大家參考,具體如下:
下載安裝xampp套件
下載mod_python-3.3.1.win32-py2.5-Apache2.2.exe
下載python-2.5.4.msi
下載Django
下載MySQL-python-1.2.2.win32-py2.5.exe
1、先安裝Python-2.5.4.msi
2、安裝 Django-1.1.1-final.tar.gz 解壓開,然后解壓到某個目錄如:(D:/Dev)
在命令提示符下進入該目錄,輸入:cd D:/Dev/Django-1.1.1
再輸入命令:python setup.py install
先簡單的測試一下。
命令提示符下,輸入:python
然后輸入import django
然后輸入django.VERSION
我看到的是這樣的: >>> import django >>> django.VERSION (final 1.1.1) >>>
3、安裝 MySQL-python-1.2.2.win32-py2.5.exe
這個雙擊安裝過程中應該不會出錯。
4、安裝 mod_python-3.3.1.win32-py2.5-Apache2.2.exe
最后一個選擇目錄要安裝在apache的安裝目錄下。
5、新建項目
命令行進入c:/Python25/,執行“django-admin.py startproject myproj”,新建名為myproj的項目。
6、新建py文件
在c:/Python25/myproj目錄下新建helloWord.py:
from django.http import HttpResponsedef index(request): return HttpResponse('Hello, Django!')配置urls.py文件
from django.conf.urls.defaults import *# Uncomment the next two lines to enable the admin:# from django.contrib import admin# admin.autodiscover()urlpatterns = patterns('', # Example: # (r'^myproj/', include('myproj.foo.urls')), (r'^$', 'myproj.helloworld.index'), # Uncomment the admin/doc line below and add 'django.contrib.admindocs' # to INSTALLED_APPS 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)),)7、配置Apache的httpd.conf
添加LoadModule python_module modules/mod_python.so
編輯httpd-vhosts.conf:
Listen 81NameVirtualHost 127.0.0.1:81<VirtualHost 127.0.0.1:81> ServerName localhost:81 <Location "/"> SetHandler python-program PythonPath "['c:/python25'] + sys.path" PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE myproj.settings PythonInterpreter mysite PythonAutoReload Off PythonDebug On </Location></VirtualHost>
注:80為web端口,81為新端口 pythonpath=c:/python25
配置好后可以在http://localhost:81 訪問Django的站點目錄。
8、Django admin設置
(1) 創建admin.py在項目myproj下
from django.contrib import adminfrom more_with_admin.examples import modelsclass DocumentAdmin(admin.ModelAdmin): passclass CommentAdmin(admin.ModelAdmin): passadmin.site.register(models.Document, DocumentAdmin)admin.site.register(models.Comment, CommentAdmin)
(2) 在seettings中的INSTALLED_APPS 添加
'django.contrib.admin'
(3) 在urls中添加
from django.contrib import admin admin.autodiscover() 與
(r'^admin/(.*)', admin.site.root),
運行python manage.py sqlall admin
(4) 運行 python manage.py runserver,將會出現以下信息
Validating models...
0 errors found.
Django version 0.96-pre, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
現在你可以訪問http://127.0.0.1:8000/admin/,登錄
9、Django 數據庫設置
創建db.py
#coding=utf-8#import os#os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'from django.conf import settingssettings.configure( DATABASE_ENGINE='mysql', DATABASE_NAME='django_demo', DATABASE_USER='root', DATABASE_PASSWORD='', DATABASE_HOST='localhost', DATABASE_PORT='', )
load_db_py
import dbfrom django.db import connectioncursor = connection.cursor ()cursor.execute ("SELECT VERSION()")row = cursor.fetchone ()print "server version:", row[0]cursor.execute ("SELECT * from django_site")row1 = cursor.fetchall ()print row1cursor.close ()connection.close ()如果出現結果,說明數據庫讀取成功。
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選