Django开发环境搭建(Windows+Python2.6)
一、需要下载的工具:
Django-1.1.1 (非常有用的教程:Django Step by Step)
mod_python-3.3.1.win32-py2.6-apache2.2
二、安装上面的工具
1. 安装python2.6
安装到 C:/Python26
接下来添加环境变量到path:c:\Python26;c:\Python26\Scripts;
2. 安装PIL
3. 安装django
转到django的解压目录
如:
cmd
cd /d e:/django/Django-1.1.1
继续在命令行输入如下命令开始安装django:
python setup.py install
(
让我们来试试:
建立目录d:\django_test
cd /d d:
mkdir django_test
cd django_test
django-admin.py startproject mysite
好了,你可以在django_test/mysite目录下面产生了4个.py文件:manage.py, __init__.py, urls.py, settings.py。
cd mysite
manage.py runserver
在浏览器中输入:
你看到Welcome to Django的页面了吗?
修改配置项目的配置文件:settings.py
首先,在mysite目录下面创建两个目录
media: 用于存放媒体文件
templates: 用于存放模板文件
mkdir media
mkdir templates
用写字板打开settings.py修改或添加:
MEDIA_ROOT = 'd:/django_test/mysite'
STATIC_PATH = 'd:/django_test/mysite/media'
TEMPLATE_DIRS = (
"d:/django_test/mystie/tempates",
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
)
4. 安装apache
5. 安装mod_python(如果你在windows 7下安装遇到问题请Email: nanac.xu@gmail.com)
需要选择apache的安装路径
6. 配置Apache的httpd.conf配置文件:
首先,在“Dynamic Shared Object (DSO) Support”的配置下增加一行
LoadModule python_module modules/mod_python.so
这个必须手动添加。
在配置文件的尾部添加:
# mysite目录路径: d:/django_test/mysite,
# 但是对于PythonPath,必须设置成这个目录的上一级目录!
# this site url:http://localhost:80/
<Location "/">
SetHandler python-program
PythonPath "sys.path+['d:/django_test']"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonInterpreter mysite
PythonDebug On
</Location>
#Alias /site_media 是用来将 mysite的静态文件设置一个 URL 访问的别名。
Alias /site_media d:/django_test/mysite/media
<Location "/site_media/">
SetHandler None
</Location>
#Alias /media 是将 Django Admin 的静态文件设置一个 URL 的访问别名。
Alias /media c:/Django-1.1.1/django/contrib/admin/media
<Location "/media/">
SetHandler None
</Location>
#配置静态文件权限,让apache有权访问
<Directory "c:/python26/Lib/site-packages/django/contrib/admin/media">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
MaxRequestsPerChild 1
# file types we want to serve statically
# case insensative match
<LocationMatch "(?i)\.(jpg|gif|png|txt|ico|pdf|css|jpeg)$">
SetHandler None
</LocationMatch>
常见错误及解决方案见:http://isun.blog.sohu.com/88570908.html
在浏览器中输入:http://127.0.0.1/试试,看到了什么?
7. 安装mysql
安装Mysql5
修改项目的配置文件:(配置数据库的连接)
DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'db_name' # Or path to database file if using sqlite3.
DATABASE_USER = 'root' # Not used with sqlite3.
DATABASE_PASSWORD = '***' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
在mysql命令行输入:create database dbmclsite default charset utf8 collate utf8_unicode_ci
在windows命令行输入:python manage.py syncdb
出错啦!
DLL load failed: 找不到指定的模块. 解决方案:
1) libmmd.dll, libguide40.dll和libmySQL.dll三个dll文件复制到python安装目录的Lib\site-packages下。
2) file "__init__", replace:
from sets import ImmutableSet
class DBAPISet(ImmutableSet):
with
class DBAPISet(frozenset):
3) file "converters.py", remove:
from sets import BaseSet, Set
4) file "converters.py", change "Set" by "set" (IMPORTANT: only two places):
line 48: return set([ i for i in s.split(',') if i ])
line 128: set: Set2Str,
OK, Let’s try!
python manage.py syncdb
It words!
到这里,环境的搭建工作就算基本完成了。
References:
http://www.3gmatrix.cn/4/viewspace-16757.html
http://hi.baidu.com/doublelook/blog/item/e7fe3918c4b6d74d42a9ad4c.html
http://isun.blog.sohu.com/88570908.html
二、 启用管理界面及更改管理界面的外观
http://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-form
三、 国际化
http://docs.djangoproject.com/en/1.1/topics/i18n/
需要用到的gettext工具:
http://sourceforge.net/projects/gettext/
四、 支持富文本编辑
tinymce
http://tinymce.moxiecode.com/download.php
五、常用命令
MySQL:
show databases;
use db_name;
create database db_name default character set utf8 collate utf8_unicode_ci;
show create database db_name;
show tables;
show create table_name;
同步数据库:
manage.py syncdb;
国际化:
django-admin.py makemessages –l cn-zh
django-admin.py makemessages –a
django-admin.py compilemessages