2011/05/02

Django

【安裝 Django】

Ubuntu 底下安裝可以使用:sudo apt-get install python-django
Red Hat 底下安裝可以使用:yum install Django

Ubuntu 預設會把 Django 放在:/usr/local/lib/python2.*/site-packages
Red Hat 預設會把 Django 放在:/usr/lib/python2.*/site-packages

Python won't recognize Django unless it is installed in the "site-packages" directory, so instead we just create a symbolic link to the source code in our home directory.

由上文可知,其實 Django 只要放到正確的 site-packages 下,Python 就可以認到了。
比較麻煩的就是 Python 版本的差異,例如 RHEL 5 預設只有 Python 2.4。
但是 RHEL 5 底下使用 yum 安裝的所有 Python 套件,都會放到 Python 2.4 的 site-packages。
如果你執行的環境是 Python 2.6,那就很頭大了,Python 2.6 底下 import 會少一堆東西。

較好的解決方案是使用:python-virtualenv
跑題了,之後再補充。

使用 Django 原始檔來安裝(想成是 Linux 的 tarball install 吧),自由度會比較高些。

可以下載官方版本來解壓縮,或使用 Subversion 同步一份到自己的家目錄:
cd /var/www
svn co http://code.djangoproject.com/svn/django/trunk/ django_src

使用這個命令取得 Python 的 site-packages 路徑:
#注意 python、python2.4、python2.6 的差別!
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python2.4 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python2.6 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

選擇你的 site-packages 路徑(python2.4 或 python2.6),做一個 symbolic link 到那裡:
ln -s /var/www/django_src/django   你的 site-packages 路徑/django

把 django-admin.py 加到執行路徑(PATH)裡:
sudo cp /var/www/django_src/django/bin/django-admin.py /usr/local/bin

基本上這樣就安裝好 Django 了。
可以進入你的 Python 2.* 環境,測試一下:
>> import django



【建立 Django 專案】

建立一個新的 Django 專案:
cd /var/www/
django-admin.py startproject hello_django

裡面預設的結構是:
__init__.py
manage.py
setting.py
urls.py

再增加幾個資料夾到專案裡面:
cd /var/www
ln -s /var/www/django_src/django/contrib/admin/media  admin_media

cd /var/www/hello_django
mkdir media
mkdir templates

setting.py 裡面可以設置管理資訊與資料庫設定:
ADMINS = (
     ('Your Name', 'your_email@domain.com'),
)

DATABASE_ENGINE = 'mysql'            # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'databaes_name'    # Or path to database file if using sqlite3.
DATABASE_USER = 'user_name'          # Not used with sqlite3.
DATABASE_PASSWORD = 'user_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.

TEMPLATE_DIRS = ( 
    "/var/www/hello_django/templates/"    # Absolute path
)

MEDIA_ROOT = '/var/www/hello_django/media/'   # Absolute path to the directory that holds media.

# URL that handles the media served from MEDIA_ROOT. 
# Make sure to use a trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/" 
MEDIA_URL = 'http://yourdomain.com/media/'

# URL prefix for admin media -- CSS, JavaScript and images. 
# Make sure to use a trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin_media/'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
)

同步資料庫:
django-admin.py syncdb

編輯 urls.py 檔:
# Uncomment this for admin:
 (r'^admin/', include('django.contrib.admin.urls')),



【設置 Apache】




Reference:
01. http://jeffbaier.com/articles/installing-django-on-an-ubuntu-linux-server/
02. http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/
03. https://help.ubuntu.com/community/Django
04. http://nildamului.blogspot.com/2009/03/django-1.html
05. http://nildamului.blogspot.com/2009/04/django-2.html

沒有留言:

張貼留言