# Python新手使用Django架站技術實作:活用Django 2.0 Web Framework建構動態網站的16堂課 (2018)
Source : 博客來
新舊版主要的差異除了 urls.py 路由部分改寫外, 最主要的是第 14 章由原先的 "二級網域管理網站建置實務" 改為 "使用 Mezzanine 快速打造 CMS 網站", 我照其說明只用了下列四個指令就架好了一個網站:
pip3 install mezzanine
mezzanine-project mysite
python manage.py createdb
python manage.py runserver
以下就是這次的實驗記錄.
Mezzanine 意思是 '夾層', 我猜作者可能認為 Mezzanine 是 dummy 與網站架設兩者之間的夾層吧? 其官網有展示網站可試用其功能, 參考 :
# http://mezzanine.jupo.org/
教學文件參考 :
# http://mezzanine.jupo.org/docs/
以 Mezzanine 架設之網站展示參考 :
# http://mezzanine.jupo.org/sites/
原來 FireFox 網站也是用 Mezzanine 架設的, 除了可用來架設公司網站, 入口網站. 個人網站, 購物網站外, 也可以用來架設部落格, Mezzanine 可說是 Python 版的 Wordpress.
觀察 Mezzanine 測試網站原始碼, 可知它使用了 jQuery 與 Bootstrap 技術打造一個功能強大, 穩定而具有彈性的內容管理平台, 比較重要的特點如下 :
- 內建所視即所得編輯器
- 可選擇免費或付費之主題佈景
- 有使用者帳號 e-mail 驗證功能
- 可製作多語言網站 (超過 35 種語言)
- 可與第三方 Django App 整合
# https://pypi.org/project/Mezzanine/
以下就來測試看看如何使用 Mezzanine 架設網站.
1. 安裝 mezzanine 套件 :
使用 Mezzanine App 架站需先安裝 mezzanine 套件 :
pip3 install mezzanine
D:\test\python\myblog>pip3 install mezzanine
Collecting mezzanine
Downloading https://files.pythonhosted.org/packages/7f/cf/0f2cbd27edfc9568c7fad26ca217e02d209031e0298562a29040e2b75a5e/Mezzanine-4.3.1-py2.py3-none-any.whl (5.9MB)
Requirement already satisfied: requests>=2.1.0 in c:\python37\lib\site-packages (from mezzanine) (2.21.0)
Collecting requests-oauthlib>=0.4 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/a3/12/b92740d845ab62ea4edf04d2f4164d82532b5a0b03836d4d4e71c6f3d379/requests_oauthlib-1.3.0-py2.py3-none-any.whl
Collecting grappelli-safe>=0.5.0 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/3e/af/10804d792cfe4eb0029959c55169086e2d4ab02b99d88bd4d85281f48245/grappelli_safe-0.5.2-py2.py3-none-any.whl (163kB)
Collecting django-contrib-comments (from mezzanine)
Downloading https://files.pythonhosted.org/packages/95/df/e6f3f465d12db886262644c5fdf5cc998800232a164b981d418e9a78742c/django_contrib_comments-1.9.2-py2.py3-none-any.whl (403kB)
Collecting future>=0.9.0 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz (829kB)
Requirement already satisfied: pillow in c:\python37\lib\site-packages (from mezzanine) (5.4.1)
Requirement already satisfied: chardet in c:\python37\lib\site-packages (from mezzanine) (3.0.4)
Collecting tzlocal>=1.0 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/ef/99/53bd1ac9349262f59c1c421d8fcc2559ae8a5eeffed9202684756b648d33/tzlocal-2.0.0-py2.py3-none-any.whl
Requirement already satisfied: beautifulsoup4>=4.5.3 in c:\python37\lib\site-packages (from mezzanine) (4.7.1)
Collecting bleach>=2.0 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/ab/05/27e1466475e816d3001efb6e0a85a819be17411420494a1e602c36f8299d/bleach-3.1.0-py2.py3-none-any.whl (157kB)
Collecting filebrowser-safe>=0.5.0 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/77/f2/373598a98fe4643f4e2aeb99b2af5861a8cff75e0ab814f056b91f4d9c76/filebrowser_safe-0.5.0-py2.py3-none-any.whl (230kB)
Collecting django<1.12,>=1.8 (from mezzanine)
Downloading https://files.pythonhosted.org/packages/a6/2b/fbd71ae0980c899c0df70779d36c5897a6b56518eb5942ddd53b0b969b30/Django-1.11.27-py2.py3-none-any.whl (6.9MB)
Requirement already satisfied: certifi>=2017.4.17 in c:\python37\lib\site-packages (from requests>=2.1.0->mezzanine) (2018.11.29)
Requirement already satisfied: idna<2.9,>=2.5 in c:\python37\lib\site-packages (from requests>=2.1.0->mezzanine) (2.8)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\python37\lib\site-packages (from requests>=2.1.0->mezzanine) (1.24.1)
Collecting oauthlib>=3.0.0 (from requests-oauthlib>=0.4->mezzanine)
Downloading https://files.pythonhosted.org/packages/05/57/ce2e7a8fa7c0afb54a0581b14a65b56e62b5759dbc98e80627142b8a3704/oauthlib-3.1.0-py2.py3-none-any.whl (147kB)
Requirement already satisfied: six in c:\python37\lib\site-packages (from django-contrib-comments->mezzanine) (1.12.0)
Requirement already satisfied: pytz in c:\python37\lib\site-packages (from tzlocal>=1.0->mezzanine) (2018.9)
Requirement already satisfied: soupsieve>=1.2 in c:\python37\lib\site-packages (from beautifulsoup4>=4.5.3->mezzanine) (1.7.3)
Collecting webencodings (from bleach>=2.0->mezzanine)
Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Installing collected packages: oauthlib, requests-oauthlib, grappelli-safe, django, django-contrib-comments, future, tzlocal, webencodings, bleach, filebrowser-safe, mezzanine
Found existing installation: Django 2.2.5
Uninstalling Django-2.2.5:
Successfully uninstalled Django-2.2.5
Running setup.py install for future ... done
Successfully installed bleach-3.1.0 django-1.11.27 django-contrib-comments-1.9.2 filebrowser-safe-0.5.0 future-0.18.2 grappelli-safe-0.5.2 mezzanine-4.3.1 oauthlib-3.1.0 requests-oauthlib-1.3.0 tzlocal-2.0.0 webencodings-0.5.1
可見 Mezzanine 4.3.1 版使用 Django 1.11.27, 直接安裝會把原先的 Django 2.2.5 移除, 所以最好還是用 virtualenv 於虛擬環境中做測試比較好.
2. 建立專案 :
首先建立一個目錄 mezzanine 資料夾存放所有 Mezzanine 專案 :
D:\test\python>mkdir mezzanine
D:\test\python>cd mezzanine
然後下 "mezzanine-project 專案名稱" 指令來建立專案, 例如 :
mezzanine-project mysite
這會建立一個 mysite 專案 :
D:\test\python\mezzanine>mezzanine-project mysite
用 tree 指令查看檔案目錄結構 :
D:\test\python\mezzanine>tree mysite /F
列出磁碟區 Data 的資料夾 PATH
磁碟區序號為 D8A8-5056
D:\TEST\PYTHON\MEZZANINE\MYSITE
│ .gitignore
│ .hgignore
│ fabfile.py
│ manage.py
│ requirements.txt
│
├─deploy
│ crontab.template
│ gunicorn.conf.py.template
│ local_settings.py.template
│ nginx.conf.template
│ supervisor.conf.template
│
└─mysite
local_settings.py
settings.py
urls.py
wsgi.py
__init__.py
可見基本上就是 Django 的架構.
3. 建立資料庫與後台管理帳密 :
首先切換到專案目錄 mysite 底下 :
D:\test\python\mezzanine>cd mysite
然後執行下列命令來建立資料庫, 它會建立 Mezzanine 網站系統運作所需之資料表, 建完後會要求設定後台管理者帳密 :
python manage.py createdb
D:\test\python\mezzanine\mysite>python manage.py createdb
Operations to perform:
Apply all migrations: admin, auth, blog, conf, contenttypes, core, django_comments, forms, galleries, generic, pages, redirects, sessions, sites, twitter
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sites.0001_initial... OK
Applying blog.0001_initial... OK
Applying blog.0002_auto_20150527_1555... OK
Applying blog.0003_auto_20170411_0504... OK
Applying conf.0001_initial... OK
Applying core.0001_initial... OK
Applying core.0002_auto_20150414_2140... OK
Applying django_comments.0001_initial... OK
Applying django_comments.0002_update_user_email_field_length... OK
Applying django_comments.0003_add_submit_date_index... OK
Applying pages.0001_initial... OK
Applying forms.0001_initial... OK
Applying forms.0002_auto_20141227_0224... OK
Applying forms.0003_emailfield... OK
Applying forms.0004_auto_20150517_0510... OK
Applying forms.0005_auto_20151026_1600... OK
Applying forms.0006_auto_20170425_2225... OK
Applying galleries.0001_initial... OK
Applying galleries.0002_auto_20141227_0224... OK
Applying generic.0001_initial... OK
Applying generic.0002_auto_20141227_0224... OK
Applying generic.0003_auto_20170411_0504... OK
Applying pages.0002_auto_20141227_0224... OK
Applying pages.0003_auto_20150527_1555... OK
Applying pages.0004_auto_20170411_0504... OK
Applying redirects.0001_initial... OK
Applying sessions.0001_initial... OK
Applying sites.0002_alter_domain_unique... OK
Applying twitter.0001_initial... OK
A site record is required.
Please enter the domain and optional port in the format 'domain:port'.
For example 'localhost:8000' or 'www.example.com'.
Hit enter to use the default (127.0.0.1:8000):
Creating default site record: 127.0.0.1:8000 ...
Creating default account ...
Username (leave blank to use 'user'): admin (網站管理員帳號)
Email address: blablabla@ms5.hinet.net (網站管理員郵箱)
Password: (網站管理員密碼)
Password (again):
Superuser created successfully.
Installed 2 object(s) from 1 fixture(s)
Would you like to install some initial demo pages?
Eg: About us, Contact form, Gallery. (yes/no): yes (建立預設網頁)
Creating demo pages: About us, Contact form, Gallery ...
Installed 16 object(s) from 3 fixture(s)
D:\test\python\mezzanine\mysite>
這樣就架好一個網站了. 但是在瀏覽這個網站之前, 需先將專案設定檔 settings.py 中的 ALLOWED_HOSTS 設定為任何主機皆可瀏覽 :
4. 設定允許瀏覽之主機 :
開啟專案設定檔 settings.py, 大約在 94 行左右有一個 專案設定檔 ALLOWED_HOSTS 參數, 原本為一個空陣列 :
ALLOWED_HOSTS = [ ]
在陣列中加入 '*' 後存檔 :
########################
# MAIN DJANGO SETTINGS #
########################
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ['*']
5. 開啟開發伺服器瀏覽網站 :
開啟命令提示字元視窗, 以下列指令開啟開發伺服器 :
python manage.py runserver
若出現如下訊息表示伺服器已啟動成功 :
D:\test\python\mezzanine\mysite>python manage.py runserver
.....
_d^^^^^^^^^b_
.d'' ``b.
.p' `q.
.d' `b.
.d' `b. * Mezzanine 4.3.1
:: :: * Django 1.11.27
:: M E Z Z A N I N E :: * Python 3.7.2
:: :: * SQLite 3.21.0
`p. .q' * Windows 10
`p. .q'
`b. .d'
`q.. ..p'
^q........p^
''''
Performing system checks...
System check identified no issues (0 silenced).
January 03, 2020 - 00:21:22
Django version 1.11.27, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
在瀏覽器網址列輸入 http://127.0.0.1:8000/ 即可看到此專案之預設網頁了 :
Bingo! 一個專業網站馬上就架好了!
按首頁第一個超連結 "Log in to the admin interface" 以上面安裝時設定之管理者帳密登入即可進入後台管理介面的 dashboard (後台儀錶板) :
正中央就是部落格貼文區, 輸入標題與內容後按 "Save Draft" 儲存會出現文章列表頁面, status 欄顯示此文為 Draft (草稿), 必須將其切換為 Published 才會真正顯示在網站上, 按 Dashboard 右上角的 "View site" 即可瀏覽網站 :
6. 語言介面設定 :
Mezzanine 預設為英文版, 但這是指管理介面而言, 內容貼任何語言都沒有問題. Mezzanine 支援 I18N 國際化 (至少 35 種語言), 如果要將 Mezzanine 介面中文化, 需修改 Django 的 settings.py 設定檔.
開啟專案目錄 mysite 底下的 settings.py, 搜尋 "USE_I18N" 會在 130 行找到原始設定 USE_I18N = False, 將其改為 True :
USE_I18N = True
其次要修改 TIME_ZONE 時區設定 (104 行), 將原始值 'UTC' 改為 'Asia/Taipei' :
TIME_ZONE = 'Asia/Taipei'
接著修改 LANGUAGE_CODE 語言碼設定 (111 行), 將預設值 "en" 改為 "zh-tw" :
LANGUAGE_CODE = "zh-tw"
然後是修改 LANGUAGES 列表, 將預設的 ('en', _('English') 改為 ('zh-tw', _('繁體中文') :
LANGUAGES = (
('zh-tw', _('繁體中文')),
)
這樣就修改完畢了, 但最後最重要的一點是, 因為在 settings.py 中使用了中文, 所以必須在第一行添加 # coding=UTF-8 :
# coding=UTF-8
而且在存檔時必須選擇用 UTF-8 格式 :
然後回命令提示字元視窗按 Ctrl+C 停止開發伺服器, 再重新啟動伺服器 :
D:\test\python\mezzanine\mysite>python manage.py runserver
這時重新整理 Dashboard 管理網頁即變成中文介面了 :
注意, 上面 settings.py 第一行添加 # coding=UTF-8 以及用 UTF-8 存檔缺一不可, 否則會在啟動開發伺服器時出現錯誤.
7. 網站基本設定 :
在 Dashboard 後台儀表板往下拉, 有一個 "站點 (Site)" 區塊, 點其中的 "設定 (Setup)" 可進行網站基本設定, 例如網站標題 (Title), 是否允許吳帳號留言, 留言通知信箱, twitter 認證金鑰等等 :
沒有留言 :
張貼留言