今天花了一整天的时间,终于将django整合到apache了。记录一下省得以后再花时间研究。
1.安装python2.7和Apache2.2,这个网上参考的例子比较多,我就不多说了。
2.安装完成后,接下来:
3.安装mod_python,这个包需要手动生成,不想生成的可以到这下载
下载,一路根据提示选择即可。
4. 修改配置文件
打开:urls.py
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^mysite/', include('mysite.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'/admin/', admin.site.urls),
)
打开setting.py
MEDIA_ROOT = 'C:/Python27/Lib/site-packages/django/contrib/admin/media'
打开httpd.conf
LoadModule python_module modules/mod_python.so
<Directory "C:/pweb">
AllowOverride FileInfo
AddHandler mod_python .py
PythonHandler MyTest
PythonDebug On
</Directory>
<Location "/mysite/">
SetHandler python-program
PythonPath "sys.path+['c:/pweb']"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonInterpreter mysite
PythonDebug On
</Location>
Alias /admin_media c:/Python27/Lib/site-packages/django/contrib/admin/media
<Directory "C:/Python27/Lib/site-packages/django/contrib/admin/media">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
<Location "/media/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png|txt|ico|pdf|css|jpeg)$">
SetHandler None
</LocationMatch>
5.进入admin管理界面,发现成了大白页,没有了样式。
添加下面这段代码到httpd.conf中
2 < Directory "C:/Python27/Lib/site-packages/django/contrib/admin/media" >
3 AllowOverride None
4 Options None
5 Order allow,deny
6 Allow from all
7 </ Directory >
8 < Location "/media/" >
9 SetHandler None
10 </ Location >
11 < LocationMatch "\.(jpg|gif|png|txt|ico|pdf|css|jpeg)$" >
12 SetHandler None
13 </ LocationMatch >
替换你系统中,对应的路径, 注意文档中的别名 /admin_media
打开你项目的 setting.py 文件,做如下修改,路径都是对应你自己系统的安装路径
MEDIA_ROOT = 'C:/Python27/Lib/site-packages/django/contrib/admin/media'
ADMIN_MEDIA_PREFIX = '/admin_media/'
OK,重启apache后,在浏览器中键入:
http://localhost/mysite/admin/,看到什么了?
整合后完整的文件下载: