2017年7月20日 星期四

[Django] 使用Wagtail CMS建置內容管理系統

Wagtail CMS靈活性夠,具有自己的權限系統,當使用者發布文章的時候,還可提交管理員先審核後放行(可透過郵件通知),還有版本控制的功能,讓管理者知道使用者改了些什麼,這個可以改裝成會員CRM系統/Blog共筆系統/官方網站

大家可以先參考Wagtail CMS的網站 https://wagtail.io/ 

Wagtail CMS是基於Django再改開發,建議大家直接裝
Python 3.6 + Django 1.11/2.0 +  Wagtail 1.11/2.0

廢話不多說,我們來使用pip來安裝

pip install wagtail

我們假設大家已經用Django建立出project了
1. 到 settings.py 填入以下資料
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
#以下新增    
    'wagtail.wagtailforms',
    'wagtail.wagtailredirects',
    'wagtail.wagtailembeds',
    'wagtail.wagtailsites',
    'wagtail.wagtailusers',
    'wagtail.wagtailsnippets',
    'wagtail.wagtaildocs',
    'wagtail.wagtailimages',
    'wagtail.wagtailsearch',
    'wagtail.wagtailadmin',
    'wagtail.wagtailcore',


    'taggit',
    'modelcluster',

]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
 #以下新增   
    'wagtail.wagtailcore.middleware.SiteMiddleware',

    'wagtail.wagtailredirects.middleware.RedirectMiddleware',
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
#定義網站名稱
WAGTAIL_SITE_NAME = 'My Example Site'

2. 到 urls.py 填入以下資料
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls

urlpatterns = [
    ...
    url(r'^cms/', include(wagtailadmin_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),
    url(r'^pages/', include(wagtail_urls)),
    ...
]

3. 建立資料庫
python manage.py migrate

4. 建立超級使用者
python manage.py createsuperuser

5. 啟動Wagtail CMS
python manage.py runserver
6. 來快速預覽一下Wagtail CMS的介面