Using ckEditor in selected text area in django admin forms

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

Like the snippet below, ckeditor will be applied for each text field present in django form:

class ProjectAdmin(admin.ModelAdmin):

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

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

but I do not want it in a specific text field on every text field.

+3
source share
1 answer

You have several options.

I think the simplest is if you are using Django 1.2, then you need to create your own form for your administrator and use the "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, :

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
+4

Source: https://habr.com/ru/post/1747334/


All Articles