下载地址: http://httpd.apache.org/
二、安装mod_python
下载地址:http://www.modpython.org/
注:python 用的是 2.5 的,安装目录为D:/Python25
三、安装django
下载地址: http://code.djangoproject.com
D:/Python25 > python setup.py install
安装完后,检查 D:/Python25/Lib/site-packages/django/bin /django-admin.py是否存在
路径为: D:/WebSite
D:/WebSite > python D:/Python25/Lib/site-packages/django/bin/django-admin.py startproject messageboard
启动django Web服务器,使用http://localhost:8000测试是否配置成功
四、配置Apache虚拟主机
DocumentRoot "D:/WebSite/messageboard"
ServerName http://10.23.4.172:8080
<Location "/">
SetHandler python-program
PythonPath "['D:/WebSite'] + sys.path"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE messageboard.settings
PythonAutoReload Off
Pythoninterpreter messageboard
PythonDebug on
</Location>
Alias /media d:/Python25/Lib/site-packages/django/contrib/admin/media
<Location "/site_media">
SetHandler None
</Location>
SetHandler None
</Location>
</VirtualHost>
<Directory "D:/WebSite/messageboard/media">
Allow from all
Order Deny,Allow
</Directory>
<Directory "D:/WebSite/messageboard">
Allow from all
Order allow,deny
AllowOverride All
</Directory>
#################################################################
PythonPath "[' D:/WebSite'] + sys.path"注意加的是' D:/WebSite',也就是我们运行django-admin.py startproject messageboard时所在的目录
SetEnv DJANGO_SETTINGS_MODULE messageboard.settings中 messageboard是project名
Data : 数据文件
Html : 静态HTML页面目录
Images : 图片文件
messageboard : Python原代码文件
|- c01 Python模块
|- templates Python所使用的HTML模板
五、测试Apache、Python、Django
1.修改 messageboard目录下的urls.py文件
from django.conf.urls.defaults import *
urlpatterns = patterns('',
# Example:
# (r'^py/', include('py.foo.urls')),
( r'^ helloworld/$', 'py. helloworld.index' ),
# Uncomment this for admin:
# (r'^py/admin/', include('django.contrib.admin.urls')),
)
2.在 messageboard目录中创建 HelloWorld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
启动httpd服务:使用 http://10.23.4.172:8080/ helloworld测试
六、Python_MySQL安装
MySQL-python-1.2.2.win32-py2.5.exe
1.在D:/WebSite/messageboard/settings.py中配置
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'messageboard'
DATABASE_USER = 'messageboardusr'
DATABASE_PASSWORD = 'messageboardusr'
DATABASE_HOST = ''
2.初始化MySQL上的django数据库,数据库名为 messageboard,并创建django的管理员用户 messageboard
D:/WebSite/messageboard>python manage.py syncdb
Creating table auth_message
Creating table auth_group
Creating table auth_user
Creating table auth_permission
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username: messageboard
E-mail address: messageboard@mysite.com
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Message model
Installing index for auth.Permission model
Loading 'initial_data' fixtures...
No fixtures found.
七、使用django admin管理
1.修改 messageboard目录下的urls.py文件
from django.conf.urls.defaults import *
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^py/', include('py.foo.urls')),
(r'^ job/$', 'messageboard. job.views.index'),
# Uncomment this for admin:
(r'^admin/(.*)', admin.site.root),
)
2.使用http:// 10.23.4.172:8080/admin登录
如果出现问题,使用 D:/WebSite/messageboard>python manage.py syncdb再同步一次数据库
3.创建post模块
D:/WebSite/messageboard> python manage.py startapp job
在settings.py中修改
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'messageboard.job'
)
修改job中的modules.py为:
#coding=utf-8
from django.db import models
# Create your models here.
class Location(models.Model):
city = models.CharField(maxlength=50)
state = models.CharField(maxlength=50, null=True, blank=True)
country = models.CharField(maxlength=50)
def __str__(self):
if self.state:
return "%s, %s, %s" % (self.city, self.state, self.country)
else:
return "%s, %s" % (self.city, self.country)
class Admin:
list_display = ("city", "state", "country")
class Job(models.Model):
pub_date = models.DateField()
job_title = models.CharField(maxlength=50)
job_description = models.TextField()
location = models.ForeignKey(Location)
def __str__(self):
return "%s (%s)" % (self.job_title, self.location)
class Admin:
list_display = ("pub_date", "job_title", "job_description", "location")
4.使用 D:/WebSite/messageboard>python manage.py syncdb同步MySQL
在MySQL, py数据库中就有了Job和 Location表