明确区分Django和Wagtail的文件和文件夹结构的差异

Background

背景

在Python中有一种全栈框架叫做”Django”,以及基于Django的CMS※1叫做”Wagtail”。
原本有一个使用Wagtail开发的项目,但考虑到与第三方的集成不便以及与Django相比参考资料较少的问题,我打算将Wagtail的项目转换为Django。

因此,我打算在这里明确各个文件在Django和Wagtail上有何不同。

※1:内容管理系统 (Content Management System,CMS)是指整合并系统地管理网页内容中的文本、图片等数字内容,并执行必要处理(如发布)的一种系统总称。其中最为著名的是WordPress等。

配置安装

1.1 安装库

我将安装Django和Wagtail。

pip install django==3.2.9
pip install wagtail==2.14.2

1.2 项目开始

我们将使用Django和Wagtail分别开始项目。
首先,从Django开始,

django-admin startproject django_proj

接着说,wagtail

wagtail start wagtail_proj

文件结构

下面展示了Django和Wagtail的文件结构。重复的文件将用(※)表示。

django_proj/
 ├ django_proj/
 │ ├ __init__.py
 │ ├ asgi.py
 │ ├ settings.py - (※)
 │ ├ urls.py - (※)
 │ └ wsgi.py - (※)
 └ manage.py - (※)
django_proj/
 ├ home/
 │  ├ migrations/
 │  │   ├ __init__.py
 │  │   ├ 0001_initial.py
 │  │   └ 0002_create_homepage.py
 │  ├ static/
 │  │   └ css/
 │  │      └ welcome_page.css
 │  ├ templates/
 │  │   └ home/
 │  │      ├ home_page.html
 │  │      └ welcome_page.html
 │  ├ __init__.py
 │  └ models.py
 ├ search/
 │ ├ templates/
 │ │  ├ templates/
 │ │      └ search/
 │ │          └ search.html
 │ ├ __init__.py
 │ └ views.py
 ├ wagtail_proj/
 │ ├ settings/
 │ │  ├ __init__.py
 │ │  ├ base.py - (※)
 │ │  ├ dev.py
 │ │  └ production.py
 │ ├ static/
 │ │  └ css/
 │ │    └ wagtail_proj.css
 │ │  └ js/
 │ │    └ wagtail_proj.js
 │ ├ templates/
 │ │  ├ 404.html
 │ │  ├ 500.html
 │ │  └ base.html
 │ ├ __init__.py
 │ ├ urls.py - (※)
 │ └ wsgi.py - (※)
 └ .dockerifnore
 └ Dockerfile
 └ manage.py - (※)
 └ requirements.txt

每个文件之间的差异

在Django和Wagtail共享的文件中,我们会展示每个文件的区别。
(请注意,我们将省略注释部分。)

urls.py的中文翻译

以下是基于django/urls.py的wagtail/urls.py。

from django.contrib import admin
from django.urls import path
# ↓ --only wagtail--
from django.urls import include 
from django.conf import settings
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from search import views as search_views
# ↑ --only wagtail--

urlpatterns = [
    path('admin/', admin.site.urls),
    # ↓ --only wagtail--
    path('django-admin/', admin.site.urls),
    path('documents/', include(wagtaildocs_urls)),
    path('search/', search_views.search, name='search'),
    # ↑ --only wagtail--
]
# ↓ --only wagtail--
if settings.DEBUG:
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns = urlpatterns + [
    path("", include(wagtail_urls)),
]
# ↑ --only wagtail--

管理.py

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wagtail_proj.settings.dev") # wagtailの場合
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_proj.settings')# djangoの場合
    # wagtailの場合
    from django.core.management import execute_from_command_line
    # djangoの場合
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

# djangoの場合
if __name__ == '__main__':
    main()

设置.py

由于结构上的差异,我们将逐步用逐节的方式来说明。

# about import 
## djangoの場合
from pathlib import Path
## wagtailの場合
import os

# about DIR
## djangoの場合
BASE_DIR = Path(__file__).resolve().parent.parent
## wagtailの場合
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

# 共通部分
SECRET_KEY = '*********************************'
DEBUG = True
ALLOWED_HOSTS = []

# about INSTALLED_APPS
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # ↓ --only wagtail--
    'home',
    'search',
    'wagtail.contrib.forms',
    'wagtail.contrib.redirects',
    'wagtail.embeds',
    'wagtail.sites',
    'wagtail.users',
    'wagtail.snippets',
    'wagtail.documents',
    'wagtail.images',
    'wagtail.search',
    'wagtail.admin',
    'wagtail.core'
    'modelcluster',
    'taggit',
    # ↑ --only wagtail--
]

# about MIDDLEWARE
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.contrib.redirects.middleware.RedirectMiddleware', ## wagtailの場合
]

# 共通部分
ROOT_URLCONF = '<your project name>.urls'

# about TEMPLATES
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [], ## djangoの場合
        'DIRS': [os.path.join(PROJECT_DIR, 'templates'),], ## wagtailの場合
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# 共通部分
WSGI_APPLICATION = '<your project name>.wsgi.application'

# about DATABASES
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',## djangoの場合
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),## wagtailの場合
    }
}

# 共通部分
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# about static files
## djangoの場合
STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

## wagtailの場合
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, 'static'),
]

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

WAGTAIL_SITE_NAME = "wagtail_proj"

BASE_URL = 'http://example.com'

wsgi.py -> wsgi文件

没有任何区别。

广告
将在 10 秒后关闭
bannerAds