Need to add a css class to an input field in Django admin

1,827 views
Skip to first unread message

Brandon Taylor

unread,
Jul 25, 2008, 4:37:03 PM7/25/08
to Django users
Hi everyone,

Does anyone know how to add a css class to an input field in newforms-
admin? I have seen how to specify a custom widget for a form object,
but not using the admin.

Specifically, I have one text area that I would like to enable TinyMCE
editor on. TinyMCE provides a way to enable/disable the editor based
on textareas with or without a specified css class.

Help appreciated!
Brandon

Brandon Taylor

unread,
Jul 27, 2008, 8:29:02 PM7/27/08
to Django users
I ended up just adding the class to the textarea with jQuery and then
running my TinyMCE init which worked perfectly. Hope this helps
someone else.

b

Daniel Roseman

unread,
Jul 28, 2008, 5:03:47 AM7/28/08
to Django users
The 'official' way is to use exactly the same principles as with a
form. You just define a custom model form, overriding the particular
field, then use that form in the admin options for that model:

class MyCustomForm(forms.ModelForm):
myfield =
forms.CharField(widget=forms.TextField(attrs={'class':'myclass'}))

class MyCustomAdmin(admin.ModelAdmin):
form = MyCustomForm

admin.site.register(MyModel, MyCustomAdmin)


Another way to do it would be to put the custom class on an entire
fieldset, rather than the field:

class MyCustomAdmin(admin.ModelAdmin):
fieldsets = (
('My custom fields', {
'fields': ('myfield',)
'classes': ('myclass',)
}
),
('My other fields', {
'fields': ('field1', 'field2', 'field3')
}
),
)


--
DR.

AndyB

unread,
Jul 28, 2008, 6:34:29 AM7/28/08
to Django users
You might not need to. Rather than classes I used the existing ID's to
specify which elements to add TinyMCE to:
tinyMCE.init(
mode : "exact",
elements : "id_description,id_access_info,id_parking_info",
...
});

Andy Baker

Brandon Taylor

unread,
Jul 29, 2008, 10:28:43 PM7/29/08
to Django users
Thanks for the great advice guys. I didn't even know TinyMCE had an
'exact' mode.

b

On Jul 28, 5:34 am, AndyB <andy...@gmail.com> wrote:
> You might not need to. Rather than classes I used the existing ID's to
> specify which elements to addTinyMCEto:tinyMCE.init(
>         mode : "exact",
>         elements : "id_description,id_access_info,id_parking_info",
> ...
>
> });
>
> Andy Baker
>
> On Jul 25, 9:37 pm, Brandon Taylor <btaylordes...@gmail.com> wrote:
>
> > Hi everyone,
>
> > Does anyone know how to add a css class to an input field in newforms-
> > admin? I have seen how to specify a custom widget for a form object,
> > but not using the admin.
>
> > Specifically, I have one text area that I would like to enableTinyMCE
> > editor on.TinyMCEprovides a way to enable/disable the editor based
Reply all
Reply to author
Forward
0 new messages