繁体   English   中英

从 ckeditor_uploader.fields 导入 RichTextUploadingField

From ckeditor_uploader.fields import RichTextUploadingField

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

从 pip 安装 CKEditor 后,我尝试从模型导入 CKEditor 但它一直给我ImportError cannot import name get_default_renderer

在我彻底遵循 CKEditor 文档之后,它仍然给我错误。 是我的 Django 1.10 版问题还是彻底的 5.6.1 版问题​​? 有人可以给我方向吗?

模型.py

from django.db import models
from django.conf import settings
from django.utils import timezone
from ckeditor_uploader.fields import RichTextUploadingField

# Create your models here.

class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete= models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.RichTextUploadingField()
    image = models.ImageField()
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank= True, null = True)

视图.py

from django.shortcuts import render, get_object_or_404,  redirect
from django.utils import timezone
from .models import Post
from .forms import CommentForm
# Create your views here.

def post_list(request):
  posts =   Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
return render(request, 'blog/post_list.html', {'posts':posts })

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
if request.method == 'POST':
    form = CommentForm(request.POST)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.post = post
        comment.save()
        return redirect('blog:post_detail', pk=post.pk)
else:
    form = CommentForm()
return render(request, 'blog/post_detail.html', {'post':post, 'form':form }, )

设置.py

INSTALLED_APPS = [
  'app',
  'cart',
  'orders',
  'blog',
  'ckeditor_uploader',
  # Add your apps here to enable them
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
]

CKEDITOR_JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'

CKEDITOR_UPLOAD_PATH = 'uploads/'

CKEDITOR_IMAGE_BACKEND = 'pillow'

#CKEDITOR_CONFIGS = {
#    'default':{
#        'toolbar': None,
#        }
#    }

网址.py

from django.conf.urls import url
import django.contrib.auth.views
from django.conf.urls import include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
# admin.autodiscover()

urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^cart', include('cart.urls')),
   url(r'^orders', include('orders.urls')),
   url(r'^', include('app.urls')),
   url(r'^blog/', include('blog.urls')),
   url(r'^ckeditor/', include('ckeditor_uploader.urls')),
]

if settings.DEBUG:
   urlpatterns += static(settings.MEDIA_URL,  document_root=settings.MEDIA_ROOT)

错误页面

ImportError: cannot import name 'get_default_renderer'
Unhandled exception in thread started by <function check_errors.  <locals>.wrapper at 0x0421B4F8>
 Traceback (most recent call last):
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site-    packages\django\utils\autoreload.py", line 226, in wrapper
 fn(*args, **kwargs)
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site-   packages\django\core\management\commands\runserver.py", line 113, in inner_run
 autoreload.raise_last_exception()
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site- packages\django\utils\autoreload.py", line 249, in raise_last_exception
 six.reraise(*_exception)
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
 raise value.with_traceback(tb)
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site- packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site-  packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site- packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site- packages\django\apps\config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32 \lib\importlib\__init__.py", line 126, in import_module
 return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 978, in _gcd_import
 File "<frozen importlib._bootstrap>", line 961, in _find_and_load
 File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 678, in exec_module
 File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
 File "C:\Users\stylop\source\repos\alfiez\alfiez\blog\models.py", line 4, in <module>
from ckeditor_uploader.fields import RichTextUploadingField
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\ckeditor_uploader\fields.py", line 3, in <module>
 from ckeditor import fields
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\ckeditor\fields.py", line 6, in <module>
 from .widgets import CKEditorWidget
 File "C:\Users\stylop\AppData\Local\Programs\Python\Python36-32\lib\site- packages\ckeditor\widgets.py", line 12, in <module>
 from django.forms.widgets import get_default_renderer
 ImportError: cannot import name 'get_default_renderer'
2 个回复

如果您只想拥有一个简单的 WYSIWYG 编辑器。 django-ckeditor 非常适合您。

pip install django-ckeditor

添加ckeditor到你的INSTALLED_APPSsettings.py

运行collectstatic管理命令: $ ./manage.py collectstatic. 这会将静态 CKEditor 所需的媒体资源复制到 STATIC_ROOT 设置给出的目录中

在您的models.py import from ckeditor.fields import RichTextField并像这样直接使用它

class Foo(models.Model):
   content = RichTextField()

from ckeditor.fields import RichTextFormField导入你的forms.py并直接使用它

class FooForm(forms.ModelForm):
    class Meta:
          model = Foo
          fields = ['content']
          widgets = {
             'content': RichTextFormField(),
          }

你应该有这样的东西ck-editor 与 Django-Admin 一起工作 有关更多配置,您可以将其添加到settings.py并配置它以适合您想要的

# CKEditor Configuration Settings

CKEDITOR_CONFIGS = {
    'default': {
        # 'toolbar': 'Custom',
        'width': '461px',
        'height': 'auto',
        # 'toolbar_Custom': [
        #     ['Bold', 'Italic', 'Underline'],
        #     ['NumberedList', 'BulletedList'],
        # ],
    }
}

设置CKEditor和设施上传图片

  1. pip install django-ckeditor

  2. 在文件 setting.py 中添加 ckeditor 和 ckeditor_uploader

INSTALLED_APPS = [ ... 'ckeditor', 'ckeditor_uploader', ... ]

  1. 在文件 setting.py 中添加下面的代码

    CKEDITOR_CONFIGS = { 'portal_config': { # 'skin': 'moono', # 'skin': 'office2013', 'toolbar_Basic': [ ['Source', '-', 'Bold', 'Italic'] ], 'toolbar_YourCustomToolbarConfig': [ {'name': 'document', 'items': [ 'Source', '-', 'Save', 'NewPage', 'Preview', 'Print', '-', 'Templates' ]}, {'name': 'clipboard', 'items': [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ]} , {'name': 'editing', 'items': ['Find', 'Replace', '-', 'SelectAll']}, {'name': 'forms', 'items': [ 'Form' , 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ]}, '/', {'name': 'basicstyles', 'items ': [ '粗体', '斜体', '下划线', 'Strike', '下标', '上标', '-', 'RemoveFormat' ]}, {'name': 'paragraph', 'items': [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', ' JustifyBlock', '-', 'BidiLtr', 'BidiRtl', 'Language' ]}, {'name': 'links', 'items': ['Link' , 'Unlink', 'Anchor']}, {'name': 'insert', 'items': [ 'Image', 'Table', 'Horizo​​ntalRule', 'Smiley', 'SpecialChar', 'PageBreak', ' iframe']}, '/', {'name': 'styles', 'items': ['Styles', 'Format', 'Font', 'FontSize']}, {'name': 'colors', 'items': ['TextColor', 'BGColor']}, {'name': 'tools', 'items': ['Maximize', 'ShowBlocks']}, {'name': 'about', 'items ': ['About']}, '/', # 把这个强制放在新行的下一个工具栏 {'name': 'yourcustomtools', 'items': [ # 把你的 editor.ui.addButton 的名字放在这里 '预览', '最大化',

     ]}, ], 'toolbar': 'YourCustomToolbarConfig', # put selected toolbar config here # 'toolbarGroups': [{ 'name': 'document', 'groups': [ 'mode', 'document', 'doctools' ] }], # 'height': 291, # 'width': '100%', # 'filebrowserWindowHeight': 725, # 'filebrowserWindowWidth': 940, # 'toolbarCanCollapse': True, # 'mathJaxLib': '//cdn.mathjax.org/mathjax/2.2-latest/MathJax.js?config=TeX-AMS_HTML', 'tabSpaces': 4, 'extraPlugins': ','.join([ 'uploadimage', # the upload image feature # your extra plugins here 'div', 'autolink', 'autoembed', 'embedsemantic', 'autogrow', # 'devtools', 'widget', 'lineutils', 'clipboard', 'dialog', 'dialogui', 'elementspath' ]),

    } }

  2. 在模型中

    从 ckeditor_uploader.fields 导入 RichTextUploadingField

    class Article(models.Model): body = RichTextUploadingField(config_name='portal_config')在此输入图片描述

1 将新的Uploader添加到Ckeditor5

如何向ckeditor5添加新的上传器功能,例如上传音频或视频? 我尝试使用ckeditor5 doc,但目前尚不清楚。 我将这个vue文件用于ckeditor5。 在此文件中,我为项目使用了自定义的uploadadapter,但是现在我不知道如何以这种态度上传其他类型的文件,例如音频 ...

4 ckeditor_uploader动态图像上传路径

我有一个Django应用程序,该应用程序要求用户每个文档上传多个图像。 该应用程序已安装django-ckeditor,但是上载的图像最终位于CKEDITOR_UPLOAD_PATH设置所设置的同一文件夹中。 我希望基于URL方案的路径是动态的。 例如,在https://myapp/report/ ...

5 RichTextUploadingField 未反映到模型

所以我一直从这个网站一步一步地跟踪到integration-ckeditor-in-django,但它没有在我的models.py中阅读/反映 这是我遵循的一步一步: 第1步: 第2步: 第 3 步:在主项目文件夹 urls.py 中: 在models.py中 迁移时出现错误 ...

9 如何通过jQuery Ajax导入CKEditor?

我正在做一个简单的数据网格应用,当我单击表中的一行时,我希望能够导入带有CKEditor的文本框。 现在,当我导入.php页面时,什么都没有显示。 至于所有其他已导入的html部分,页面上已在运行的JS并不引用导入的html。 对于其他代码,我只是将JS重写为要导入的文件,但是当我使用 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM