Apache_Python Django 配置


                                                一.安装Apache

下载地址:
http://httpd.apache.org/
apache_2.2.3-win32-x86-no_ssl.msi
二、安装mod_python
下载地址:
http://www.modpython.org/
mod_python-3.3.1.win32-py2.5-Apache2.2.exe
注:python用的是2.5的,安装目录为D:\Web2.0\Python25
三、安装django
下载地址:http://code.djangoproject.com
D:\Web2.0\Django-0.96.1>python setup.py install
安装完后,检查D:\Web2.0\Python25\Scripts\django-admin.py是否存在
四、配置WebSite目录
MySite路径为:D:\Web2.0\MySite
D:\Web2.0\MySite>python D:\Web2.0\Python25\Scripts\django-admin.py startproject py

D:\Web2.0\MySite\py>python manage.py runserver
启动django Web服务器,使用http://localhost:8000测试是否配置成功
四、配置Apache虚拟主机
DocumentRoot
"D:/Web2.0/MySite"
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
###############################################################
#
# Python Module 配置
# PeiZhengfeng 2008.04.26
#
###############################################################
MaxRequestsPerChild 1
LoadModule python_module modules/mod_python.so
NameVirtualHost 127.0.0.1:80
   
        SetHandler python-program
        PythonPath "['D:/Web2.0/MySite'] + sys.path"
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE py.settings
        PythonAutoReload Off
        PythonDebug On
   
#################################################################
MySite目录说明:
Data : 数据文件
Html :  静态HTML页面目录
Images : 图片文件
Py :
Python原代码文件
|- c01 Python模块
|- templates Python所使用的HTML模板
五、测试Apache、Python、Django
1.修改py目录下的urls.py文件
from django.conf.urls.defaults import *
urlpatterns = patterns('',
    # Example:
    # (r'^py/', include('py.foo.urls')),
    ( r'^py/$',      'py.helloworld.index' ),
    # Uncomment this for admin:
    # (r'^py/admin/', include('django.contrib.admin.urls')),
)
2.在py目录中创建HelloWorld.py
from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, Django.")
启动httpd服务:使用http://localhost/py 测试
六、Python_MySQL安装
MySQL-python-1.2.2.win32-py2.5.exe
1.在D:\Web2.0\MySite\py\settings.py中配置
DATABASE_ENGINE = 'mysql'      
DATABASE_NAME = 'py'           
DATABASE_USER = 'root'        
DATABASE_PASSWORD = 'root'     
DATABASE_HOST = ''            
DATABASE_PORT = '3306'     
2.初始化MySQL上的django数据库,数据库名为py,并创建django的管理员用户py
D:\Web2.0\MySite\py>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: py
E-mail address: zhengfeng_pei@foxmail.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.修改py目录下的urls.py文件
from django.conf.urls.defaults import *
urlpatterns = patterns('',
    # Example:
    # (r'^py/', include('py.foo.urls')),
    ( r'^py/$',      'py.helloworld.index' ),
    # Uncomment this for admin:
    (r'^py/admin/', include('django.contrib.admin.urls')),
)
2.使用http://localhost:8000/py/admin登录
如果出现问题,使用D:\Web2.0\MySite\py>python manage.py
syncdb再同步一次数据库
3.创建jobs模块
D:\Web2.0\MySite\py>python manage.py startapp jobs
在settings.py中修改
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'py',
    'py.jobs'
)
修改jobs中的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:\Web2.0\MySite\py>python manage.py
syncdb同步MySQL
在MySQL, py数据库中就有了Job和Location表