全面解读Python Web开发框架Django

2019-10-06 13:08:57王振洲

编 译: $make

安 装: $make install

测 试: $python

参 考:

官方主页: http://www.python.org/
2.3 mod_wsgi模块的安装

下 载: http://code.google.com/p/modwsgi/  (选择3.3版本)

解压缩 : $tar xvfz mod_wsgi.X.Y.tar.gz

$cd mod_wsgi.X.Y

编译配置: $././configure –with-apxs=/usr/local/apache2/bin/apxs –with-python=/usr/local/bin/python # 指定Apache2的模块编译程序和Python解析器

编 译: $make

安 装: $make install

测 试: $python

2.3.1  配置Apache(修改/usr/local/apche2/confi/httpd.conf)

# 加载wsgi模块
LoadModule wsgi_module modules/mod_wsgi.so
....
# HTTP请求处理脚本
WSGIScriptAlias /test /home/xxx/www/test.wsgi
<Directory "/home/xxx/www">
Order allow, deny
Allow from all
</Directory>

2.3.2 编写test.wsgi(WSGI标准: http://www.python.org/dev/peps/pep-3333/ )

def application(environ, start_response):
 status = '200 OK'
 output = 'Hello World!'

 response_headers = [('Content-type', 'text/plain'),
  ('Content-Length', str(len(output)))]
 start_response(status, response_headers)

 return [output]

2.3.3  重启apche2

在任意网络浏览器中输入:http://www.mysite.com/test。看到“Hello World!”,恭喜你成功安装了WSGI模块。

参 考:

官方主页: http://code.google.com/p/modwsgi/
安装文档: http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
配置文档: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
WSGI文档: http://www.python.org/dev/peps/pep-3333/

2.4 Django的安装

下 载: https://www.djangoproject.com/download/  (选择1.4版本)

解压缩 : $tar xvfz Django-1.4.tar.gz

$cd Django-1.4

安 装: $python setup.py install

测 试:

$python
>>> import django
>>> print(django.get_version())

参 考:

官方主页: https://www.djangoproject.com/
安装文档: https://docs.djangoproject.com/en/1.4/intro/install/
快速入门: https://docs.djangoproject.com/en/1.4/intro/tutorial01/

3. Django中文支持

Django使用的是UTF-8编码,所以对于国际化支持不成问题。因为初次玩Django,中文显示乱,折腾死人了(一直在用的的mysql默认字符串是latin1编码,vim默认保存的文件编码为ascii)。最终得出结论,如果中文显示乱码,或者Django报错(… unicode …blabla…),请检查:

Django的设置。打开自己项目的settings.py,LANGUAGE_CODE=”zh_CN” ?FILE_CHARSET='UTF-8′ ?DEFAULT_CHARSET='utf-8′?
查看自己项目所有的文件编码是否以UTF-8编码保存的?确保.py文件第一行要加上:#-*-  coding:utf-8 -*- ?
HTML模板文件head部分,添加<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8″/>