本文实例讲述了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:
1
2
3
|
from django.http import HttpResponse
def index(request):
return HttpResponse( 'Hello, Django!' )
|
配置urls.py文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Listen 81
NameVirtualHost 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下
1
2
3
4
5
6
7
8
|
from django.contrib import admin
from more_with_admin.examples import models
class DocumentAdmin(admin.ModelAdmin):
pass
class CommentAdmin(admin.ModelAdmin):
pass
admin.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
1
2
3
4
5
6
7
8
9
10
11
12
|
#coding=utf-8
#import os
#os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'
from django.conf import settings
settings.configure(
DATABASE_ENGINE = 'mysql' ,
DATABASE_NAME = 'django_demo' ,
DATABASE_USER = 'root' ,
DATABASE_PASSWORD = '',
DATABASE_HOST = 'localhost' ,
DATABASE_PORT = '',
)
|
load_db_py
1
2
3
4
5
6
7
8
9
10
11
|
import db
from django.db import connection
cursor = connection.cursor ()
cursor.execute ( "SELECT VERSION()" )
row = cursor.fetchone ()
print "server version:" , row[ 0 ]
cursor.execute ( "SELECT * from django_site" )
row1 = cursor.fetchall ()
print row1
cursor.close ()
connection.close ()
|
如果出现结果,说明数据库读取成功。
希望本文所述对大家Python程序设计有所帮助。