python django helloworld

1.下载python-2.7.msi、Django-1.3.tar.gz

默认安装python2.7配置好python_home,安装django,解压运行pythoninstall

2.下载httpd-2.2.22-win32-x86-no_ssl.msi、mod_wsgi-win32-ap22py27-3.3.so

http://httpd.apache.org

http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-win32-ap22py27-3.3.so

默认安装apache、copymod_wsgi-win32-ap22py27-3.3.so到apache/modules下

Apache的httpd.conf文件中最后加入以下两行

LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-3.3.so

Include "D:/python/djangoProject/myFirstDjango/apache/apache_django_wsgi.conf"

其中myFirstDjango是在D:/python/djangoProject

创建的django项目,django-admin.pystartprojectmyFirstDjango

在myFirstDjango目录下创建一个apache文件夹,添加下面2个文件:

apache_django_wsgi.conf和django.wsgi。

apache_django_wsgi.conf文件内容:

# 设置django admin静态资源的访问路径

Alias /static/ "D:/python/djangoProject/static/"

<Directory "D:/python/djangoProject/static">

Allow from all

</Directory>

# 设置root,不要使用"^/"

WSGIScriptAlias / "D:/python/djangoProject/myFirstDjango/apache/django.wsgi"

<Directory "D:/python/djangoProject/myFirstDjango/apache/">

Allow from all

</Directory>

django.wsgi内容:

import os
 
import sys

 
#Calculate the path based on the location of the WSGI script.
 
apache_configuration= os.path.dirname(__file__)
 
project = os.path.dirname(apache_configuration)
 
workspace = os.path.dirname(project)
 
sys.path.append(workspace)

 
os.environ['DJANGO_SETTINGS_MODULE'] = 'myFirstDjango.settings'
 
import django.core.handlers.wsgi
 
application = django.core.handlers.wsgi.WSGIHandler()

3.在项目下执行pythonmanagestartappmyFirst

修改settings.py中INSTALLED_APPS加入'myFirst',

在myFirst目录下创建hello.py

 
from django.http import HttpResponse
def say(request,rsponse):
    return HttpResponse("Hello, World."+rsponse)

修改urls.py

加入(r'^myFirst/(\w+)/$','myFirstDjango.myFirst.hello.say'),

4.启动apache,

在myFirstDjango下启动应用

pythonmanage.pyrunserver

5.http://localhost:8000/myFirst/hi/

显示Hello,World.hi

hello,world成功结束

相关推荐