Python_windows python2.7 django 開發環境搭建

來源:互聯網
上載者:User

一、需要下載的工具:

python2.7

PIL-1.1.6.win32-py2.7

Django book 2.0 中文版 (非常有用的教程:Django Step by Step)

apache_2.2.14

mod_python-3.3.1.win32-py2.6-apache2.2

MySQL

MySQL-python-1.2.2

libguide40.dll

libmmd.dll

libMySQL.dll

二、安裝上面的工具

1. 安裝python2.7

安裝到 d:/Python27

接下來添加環境變數到path:d:\Python27;d:\Python27\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

在瀏覽器中輸入:

http://127.0.0.1:8000/

你看到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/

SetHandler python-program
PythonPath “sys.path+['d:/django_test']“
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonInterpreter mysite
PythonDebug On

#Alias /site_media 是用來將 mysite的靜態檔案設定一個 URL 訪問的別名。
Alias /site_media d:/django_test/mysite/media

SetHandler None

#Alias /media 是將 Django Admin 的靜態檔案設定一個 URL 的訪問別名。
Alias /media c:/Django-1.1.1/django/contrib/admin/media

SetHandler None

#配置靜態檔案許可權,讓apache有權訪問

AllowOverride None
Options None
Order allow,deny
Allow from all

MaxRequestsPerChild 1
# file types we want to serve statically
# case insensative match

SetHandler None

常見錯誤及解決方案見:http://isun.blog.sohu.com/88570908.html

 

在瀏覽器中輸入:http://127.0.0.1/ 試試,看到了什嗎?

7. 安裝mysql

安裝Mysql5

安裝MySQL-python-1.2.2

修改項目的設定檔:(設定資料庫的串連)

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.