一、安裝:
pip install django-ckeditor
安裝django-ckeditor庫
https://github.com/django-ckeditor/django-ckeditor
GitHub主頁
這個庫上傳圖片是依賴pillow的
由於之前已經安裝了pillow
所以不用再pip install pillow了
二、配置文件demo/settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'db@02^k!pw$6kx*0$+9#%2h@vro-*h^+xs%5&(+q*b181&o$)l'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'product.apps.ProductConfig',
'xadmin',
'crispy_forms',
'reversion',
# 添加django-xadmin
'import_export',
# 導入導出
'ckeditor',
'ckeditor_uploader',
# 富文本編輯器
]
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',
]
ROOT_URLCONF = 'demo.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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 = 'demo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'demo',
'HOST': '192.168.1.106',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'Abcdef@123456',
}
}
# MySQL數據庫配置
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'zh-hans'
# 簡體中文界面
TIME_ZONE = 'Asia/Shanghai'
# 亞洲/上海時區
USE_I18N = True
USE_L10N = True
USE_TZ = False
# 不使用國際標准時間
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# 定義靜態文件的目錄
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# 定義圖片存放的目錄
IMPORT_EXPORT_USE_TRANSACTIONS = True
# 在導入數據時使用數據庫事務,默認False
CKEDITOR_BASEPATH = os.path.join(BASE_DIR, "/static/ckeditor/ckeditor/")
# 配置CKEditor的模板路徑
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
'height': 300,
'width': 900,
},
}
# 使用默認的主題名稱
CKEDITOR_UPLOAD_PATH = "uploads/"
# 配置圖片存儲的目錄,不用創建
# 默認使用MEDIA_ROOT,所以路徑是media/uploads
CKEDITOR_RESTRICT_BY_DATE = True
# 按年/月/日的目錄存儲圖片
CKEDITOR_BROWSE_SHOW_DIRS = True
# 按存儲在其中的目錄對圖像進行分組,並按日期排序
CKEDITOR_IMAGE_BACKEND = "pillow"
# 啟用縮略圖
三、復制資源文件:
python manage.py collectstatic
拷貝靜態文件
此時可看到static目錄下面新增了static/ckeditor目錄
四、路由轉發demo/urls.py:
import xadmin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
# path('admin/', admin.site.urls),
path('admin/', xadmin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
# 添加CKEditor的URL映射
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# 配置圖片文件url轉發
五、模型字段product/models.py:
新增了產品詳情字段
from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models
# Create your models here.
class ProductInfo(models.Model):
# 產品表
product_name = models.CharField(max_length=32, verbose_name="產品名稱")
# 產品名稱
product_picture = models.ImageField(blank=True,
null=True,
upload_to="pictures/%Y%m%d",
max_length=255,
verbose_name="產品圖片")
# 產品圖片,數據庫里面存儲的是圖片的相對路徑
product_describe = models.CharField(max_length=255, verbose_name="產品描述")
# 產品描述
product_manager = models.CharField(max_length=11, verbose_name="產品經理")
# 產品經理
product_detail = RichTextUploadingField(verbose_name="產品詳情", default="")
# 產品詳情,帶有上傳圖片功能的富文本編輯器
create_time = models.DateTimeField(auto_now_add=True, verbose_name="創建時間")
# 創建時間
update_time = models.DateTimeField(auto_now=True, blank=True, null=True, verbose_name="修改時間")
# 修改時間
class Meta:
db_table = 'product_info'
# 設置表名,默認表名是:應用名稱_模型類名
# 帶有應用名的表名太長了
verbose_name = '產品列表'
verbose_name_plural = "產品列表"
def __str__(self):
return self.product_name
六、admin后台頁面:
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
啟動服務