【问题标题】:Can't use CKEDITOR in django template无法在 django 模板中使用 CKEDITOR
【发布时间】:2017-07-06 06:12:26
【问题描述】:

我正在使用没有模型类的表单。

class ClubWallForm(forms.Form):

    title = forms.CharField(label='Post title', max_length=100)
    description = RichTextField()
    imgURL = forms.CharField(label='Post image url', max_length=100)
    filefield = forms.FileField( label='Select a file',validators=[validate_file_extension] )

在我的模板中我使用了

`        {% for field in form %}
            <div class="fieldWrapper">
                {{ field.errors }}
                {{ field.label_tag }}: {{ field }}
            </div>
        {% endfor %}`

CKEDITOR 没有显示在我的模板中。 这就是我得到的。

在我的视图中,它尝试使用 ipdb 并发现该表单只有字段 title imgurlfilefield

【问题讨论】:

    标签: python django ckeditor django-ckeditor


    【解决方案1】:

    您还可以使用小部件调整
    首先安装它

    pip install django-widget-tweaks
    

    然后将其加载到您的模板文件中

    {% load wiget_tweaks %}
    

    在您的网站标题中也使用this

    <script src="https://cdn.ckeditor.com/4.16.1/standard/ckeditor.js"></script>
    

    现在你可以在forms.py 中将你的类设置为你的表单而无需更改它 所以模板文件应该是这样的

    {{ form.description|add_class:"ckeditor" }}
    

    第二种方法是: 只需在模板文件中使用此代码

    {{ form.media }}
    {{ form.description }}
    

    【讨论】:

      【解决方案2】:

      你有两个问题:

      1. RichTextField 是一个 model 字段。 RichTextFormField 是对应的 form 字段。
      2. 您没有处理模板中的表单媒体。这是in the documentation 的概述。

      【讨论】:

        【解决方案3】:

        最后我得到了朋友的答复。 是替换

        description = RichTextField()
        

        description = forms.CharField(label='Description',
                           widget=forms.Textarea(attrs={'class': 'ckeditor'}))
        

        forms.py里面 它正在工作

        【讨论】:

        • 我不会那样做的。您正在完成 django-ckeditor 提供的表单字段完成的工作的一半,其方式是将类名与字段联系起来。如果 ckeditor 决定更改类名,您可以再次修复它。另外,您无法更改配置。
        猜你喜欢
        • 2012-01-12
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-25
        • 2014-05-02
        • 2021-06-16
        相关资源
        最近更新 更多