您的位置:首页 > 运维架构 > Apache

Apache_Python Django 配置

2009-03-09 22:29 417 查看
 

一.安装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:/Python25

三、安装django
下载地址:http://code.djangoproject.com

D:/Python25>python setup.py install

安装完后,检查D:/Python25/Lib/site-packages/django/bin/django-admin.py是否存在
 
四、配置WebSite目录
路径为:D:/WebSite

D:/WebSite>python  D:/Python25/Lib/site-packages/django/bin/django-admin.py startproject messageboard

 
D:/WebSite/messageboard>python manage.py runserver

启动django Web服务器,使用http://localhost:8000测试是否配置成功

四、配置Apache虚拟主机
 
NameVirtualHost 10.23.4.172:8080
<VirtualHost 10.23.4.172:8080>
     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 /site_media D:/WebSite/messageboard/media
     Alias /media d:/Python25/Lib/site-packages/django/contrib/admin/media
     <Location "/site_media">
         SetHandler None
      </Location>
     <Location "/media">
         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>

#################################################################
MaxRequestsPerChild设为1特别有用,不用重启Apache就可以看到程序修改的结果
PythonPath "['D:/WebSite'] + sys.path"注意加的是'D:/WebSite',也就是我们运行django-admin.py startproject messageboard时所在的目录
SetEnv DJANGO_SETTINGS_MODULE messageboard.settings中messageboard是project名

WebSite目录说明:
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 = ''            
DATABASE_PORT = '3306'    

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 *
from django.contrib import admin
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表
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息