欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Django之Apache/mod_python安装及HelloWorld 博客分类: Python DjangoPythonApache工作 

程序员文章站 2024-02-22 19:18:52
...

Django自带的服务器不是很好,改成Apache+mod_python的方式写一个HelloWorld

一、安装Apache

下载地址: http://httpd.apache.org/

apache_2.2.3-win32-x86-no_ssl.msi安装很方便,注意安装过程中要填email,否则启动报错

二、安装mod_python

下载地址:http://www.modpython.org/

mod_python-3.3.0b.win32-py2.4-Apache2.2.exe点击安装即可,注意python用的是2.4的

三、配置虚拟主机

编辑httpd.conf:

设置MaxRequestsPerChild 1,这样可以在开发阶段不用重启Apache进行测试

添加LoadModule python_module modules/mod_python.so

去掉注释Include conf/extra/httpd-vhosts.conf

假如工作目录为D:\py,cmd切换到该目录运行“django-admin.py startproject myproj

编辑httpd-vhosts.conf:

NameVirtualHost 127.0.0.1:80

<VirtualHost 127.0.0.1:80>
    <Location "/">
        SetHandler python-program
        PythonPath "['D:/py'] + sys.path"
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE myproj.settings
        PythonAutoReload Off
        PythonDebug On
    </Location>
</VirtualHost>

在D:\py\myproj下新建helloworld.py:

from django.http import HttpResponse

def index(request):
    return HttpResponse('Hello, Django!')

修改urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Example:
    # (r'^myproj/', include('myproj.apps.foo.urls.foo')),
    (r'^$', 'myproj.helloworld.index'),

    # Uncomment this for admin:
#     (r'^admin/', include('django.contrib.admin.urls')),
)

OK,启动Apache访问http://localhost