在django管理表单中的选择性文本区域上使用ckEditor [英] Using ckEditor on selective text areas in django admin forms

查看:95
本文介绍了在django管理表单中的选择性文本区域上使用ckEditor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在django管理表单中的特定文本区域上应用ckeditor而不是所有文本区域。

I want to apply ckeditor on specific textarea in django admin form not on all the text areas.

像下面的代码片段将应用ckeditor在django表单上的每个textarea

Like snippet below will apply ckeditor on every textarea present on django form:

class ProjectAdmin(admin.ModelAdmin):

    formfield_overrides = 
    {models.TextField: {'widget': forms.Textarea(attrs={'class':'ckeditor'})}, }

    class Media:
        js = ('ckeditor/ckeditor.js',)

但我希望在特定的文本区域而不是每个textarea。

but i want it on a specific textarea not on every textarea.

推荐答案

你有几个选项。

我认为最简单的是如果你使用Django 1.2,那么您必须为您的管理员创建自定义表单并使用'widgets'选项:

I think the simplest is if you use Django 1.2, then you have to create custom form for your admin and use 'widgets' option:

ProjectForm(forms.ModelForm)
    class Meta:
        model = Project
        widgets = { 
           'field_1' : forms.Textarea(attrs={'class':'ckeditor'}),
           'field_2' : forms.Textarea(attrs={'class':'ckeditor'}),
            ...
        }

如果使用旧版本的Django,您仍然可以使用自定义窗体,只需覆盖您想要ckEditor的字段,形式如下:

If you use older version of Django you can still use custom form, just override the field, in which you want ckEditor, in the form:

ProjectForm(forms.ModelForm)
    class Meta:
        model = Project

    field_1 = forms.SomeField(label=u'my label', widget=forms.Textarea(attrs={'class':'ckeditor'}))


$ b b

替代方法:

Alternative:

ProjectForm(forms.ModelForm)
    class Meta:
        model = Project

    def __init__(self, *args, **kwargs):
        super(ProjectForm, self).__init__(*args, **kwargs)
        self.fields['field_1'].widget = forms.Textarea(attrs={'class':'ckeditor'})



>最后,为所有三个选项,您设置您的ProjectAdmin使用ProjectForm:

Finally for all three options, you set your ProjectAdmin to use ProjectForm:

class ProjectAdmin(admin.ModelAdmin)
    form = ProjectForm

这篇关于在django管理表单中的选择性文本区域上使用ckEditor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆