RichTextField在Wagtail 2.0中无法使用Draftail

时间:2018-03-03 19:27:29

标签: python wagtail

我将我的网站(开发版本)更新为wagtail 2.0,我的网页的所有RichTextField属性都完全无法编辑,使用Draftail.js。我在settings.py中添加了以下几行:

WAGTAILADMIN_RICH_TEXT_EDITORS = {
    'default': {
        'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
    }
}

恢复到以前的Hallo.js编辑器,它可以正常工作。但是当我删除它时,已经保存的内容不会被打印,并且不再有编辑功能。以下是模型定义:

from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

class BlogIndexPage(Page):
    intro = RichTextField(blank=True,features=[
        'h2',
        'h3',
        'ol',
        'ul',
        'bold',
        'italic',
        'link'
    ])

content_panels = Page.content_panels + [
    FieldPanel('intro', classname="full")
]

我的settings.py文件:

import os
from decouple import config

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

# 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',
    'django_extensions',
    'django.contrib.sitemaps',

    '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',

    'homepage',
    'blog',
]

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.core.middleware.SiteMiddleware',
    'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]

ROOT_URLCONF = 'lsvwebsite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        '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 = 'site.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('NAME_DB'),
        'USER': config('USER_DB'),
        'PASSWORD': config('PASSWORD_DB'),
        'HOST': config('HOST_DB'),
        'PORT': '',
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

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

#wagtail site name
WAGTAIL_SITE_NAME = 'Site Name'

WAGTAILADMIN_RICH_TEXT_EDITORS = {
    'default': {
        'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
    }
}

看起来像是一个问题,使用新的Draftail.js编辑器......

编辑1:使用Firefox Quantum 58.0.2在Web控制台中打印错误

TypeError: o.a.createElement is not a function    draftail.js:1:241051
TypeError: window.draftail is undefined    edit:560:1

编辑2:使用Opera 51.0在Web控制台中打印错误

draftail.js:1 Uncaught TypeError: o.a.createElement is not a function
    at Object.<anonymous> (draftail.js:1)
    at t (vendor.js:1)
    at Object.<anonymous> (draftail.js:1)
    at t (vendor.js:1)
    at Object.<anonymous> (draftail.js:1)
    at t (vendor.js:1)
    at window.webpackJsonp (vendor.js:1)
    at draftail.js:1
(index):560 Uncaught TypeError: Cannot read property 'initEditor' of  undefined
    at (index):560
rangy-core.js:80 [Deprecation] The behavior that Selection.addRange() merges existing Range and the specified Range was removed. See  https://www.chromestatus.com/features/6680566019653632 for more details.
(anonymous) @ rangy-core.js:80

2 个答案:

答案 0 :(得分:2)

所以,首先我尝试清除缓存,但是没有用。 然后我记得我习惯设置firefox自动清除缓存,当它关闭时,所以我检查了我的firefox设置,它确实设置为这样。然后,我尝试了@fixgoats解决方案,我清除了我自己没有创建的静态文件并运行collectstatic,它没有用。我记得我不应该在开发环境中运行collectstatic,所以我删除它们而不再运行collectstatic,它不起作用,所以我清除了缓存(因为我没有关闭firefox在这个过程中)并且它有效。

我的猜测是,我将过时的静态文件直接存储在dev静态目录中,而firefox正在使用并缓存那些过时的文件(隐藏另一个的一个问题)。现在,一切正常,非常感谢你们所有人。

答案 1 :(得分:1)

我相信我遇到了同样的问题,至少错误信息的标题是相同的,描述非常相似。对我有用的是删除静态wagtailadmin javascript目录并再次运行collectstatic。虽然我仍然不知道究竟是什么导致了这个错误。