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:/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.settingsmessageboard是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表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值