在 django 管理表单中的选择性文本区域上使用 ckEditor

发布于 2024-09-03 00:42:40 字数 370 浏览 3 评论 0原文

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

像下面的代码片段将在 django 表单上存在的每个文本区域上应用 ckeditor:

class ProjectAdmin(admin.ModelAdmin):

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2024-09-10 00:42:41

你有几个选择。

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

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

如果您使用旧版本的 Django,您仍然可以使用自定义表单,只需覆盖您在其中的字段想要 ckEditor,格式为:

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

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

替代方案:

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:

class ProjectAdmin(admin.ModelAdmin)
    form = ProjectForm

You have couple of options.

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'}),
            ...
        }

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'}))

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'})

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

class ProjectAdmin(admin.ModelAdmin)
    form = ProjectForm
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文