简体   繁体   English

Django中TinyMCE的小部件错误

[英]Widget error with TinyMCE in Django

I'm getting this error with TinyMCE in django: 我在django中收到TinyMCE的错误:

TypeError: init () got an unexpected keyword argument 'widget' TypeError: init ()获得了意外的关键字参数“ widget”

I have followed the instructions as I found them, and don't know why the error is there. 找到它们后,我已按照说明进行操作,并且不知道为什么会出现错误。 Here is the model: 这是模型:

class Article(models.Model):
    """Represents a wiki article"""

    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=50, unique=True)
    text = models.TextField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))#      (help_text="Formatted using ReST")
    author = models.ForeignKey(User)
    is_published = models.BooleanField(default=False, verbose_name="Publish?")
    created_on = models.DateTimeField(auto_now_add=True)
    objects = models.Manager()
    published = PublishedArticlesManager()

The comment "#formatted in ReST" is because the original TextField was using restructuredText. 注释“在ReST中使用#formatted”是因为原始TextField使用的是restructuredText。 I was able to actually get tinyMCE from CDN and place a very simpel script in the head of the appropriate template. 我实际上能够从CDN获得tinyMCE,并将非常简单的脚本放置在适当模板的开头。 It loaded the wysiwyg editor, but then rendered the saved page with HTML tags visible. 它加载了所见即所得的编辑器,但是随后用HTML标记显示了已保存的页面。

So I added to form declaration as: 所以我添加到窗体声明为:

from django import forms

from models import Article, Edit, FileUploadHandler

from tinymce import models as tinymce_models

class ArticleForm(forms.ModelForm):
    class Meta:
        text = forms.Charfield(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))#(help_text="Formatted using ReST")
        model = Article
        exclude = ['author', 'slug']


class EditForm(forms.ModelForm):
    class Meta:
        model = Edit
        fields = ['summary']

class UploadImageForm(forms.ModelForm):
    class Meta:
        model = FileUploadHandler
        image = forms.ImageField()
        fields = ['title']

The editor is there, but upon save its rendering the article with html tags visible. 编辑器在那里,但是保存后呈现带有html标签的文章可见。 Why is that? 这是为什么?

widget is an attribute for form fields, not model fields. widget是表单字段而非模型字段的属性。 You need to move that setting to your form declaration (or formfield_overrides if you're trying to use it in the admin). 您需要将该设置移到您的表单声明中(如果要在管理员中使用它,则可以formfield_overrides )。

To display the marked up content without escaping the HTML tags in a later view, one way is to use the |safe built in filter. 要显示标记的内容而不在以后的视图中转义HTML标记,一种方法是使用|safe内置过滤器。

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

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