Custom CSS for newforms using ModelForm

23 views
Skip to first unread message

Mayank Dhingra

unread,
Jun 2, 2008, 2:23:31 PM6/2/08
to Django users
Hi,

I was looking to add custom css to the fields of a (new)form that I
created using ModelForm
but couldn't find any mention or discussion on it.

Does any know know how can I achieve it ?

Thanks in advance,

mw

unread,
Jun 2, 2008, 5:45:24 PM6/2/08
to Django users
I'm not entirely sure I understand the question, but a form generated
by newforms and rendered through a template can easily just have a CSS
file attached to it like you would normal HTML.

I just have a standard <link rel="stylesheet" type="text/css"
href="includes/css/form.css" /> at the top of a template that I pass
the modelform created form to.

Eric Abrahamsen

unread,
Jun 3, 2008, 12:14:47 AM6/3/08
to django...@googlegroups.com
Hey there,

You probably want this:
http://www.djangoproject.com/documentation/newforms/#customizing-widget-instances

You'll have to specify the field types for your model, then widget
types for each field, and the extra attributes go in the widget.

Yrs,
E

Mayank Dhingra

unread,
Jun 3, 2008, 3:02:17 AM6/3/08
to Django users
Actually I wanted to know how to

Say I have a model "Model_1" and I made a form from that model using
ModelForm
now how am I supposed to add custom CSS to the fields of this model?

class Model_1Form(ModelForm):
class Meta:
model = Model_1
exclude = ('hash')
def save(self, request):
something something

Let me know if I missed something.
Thanks

On Jun 3, 9:14 am, Eric Abrahamsen <gir...@gmail.com> wrote:
> Hey there,
>
> You probably want this:http://www.djangoproject.com/documentation/newforms/#customizing-widg...

Eric Abrahamsen

unread,
Jun 3, 2008, 4:50:01 AM6/3/08
to django...@googlegroups.com
Yes, that was it. ModelForms are nice because you don't have to
specify the fields to put in the form, it does that automatically.
However, as soon as you want to customize the widgets being used for
the fields, you _do_ have to specify the fields in your ModelForm, and
put the classes and whatnot into the widget. So in your case, your
form will have to do something like this:

class Model_1Form(ModelForm):
title =
forms.CharField(widget=forms.TextInput(attrs={'class':'super'}))
# other fields that need to be customized


class Meta:
model = Model_1

# etc

That ought to do it...

E

Mayank Dhingra

unread,
Jun 4, 2008, 2:58:28 PM6/4/08
to Django users
Thanks Eric that did the job


On Jun 3, 1:50 pm, Eric Abrahamsen <gir...@gmail.com> wrote:
> Yes, that was it. ModelForms are nice because you don't have to
> specify the fields to put in the form, it does that automatically.
> However, as soon as you want to customize the widgets being used for
> the fields, you _do_ have to specify the fields in yourModelForm, and
> put the classes and whatnot into the widget. So in your case, your
> form will have to do something like this:
>
> class Model_1Form(ModelForm):
> title =
> forms.CharField(widget=forms.TextInput(attrs={'class':'super'}))
> # other fields that need to be customized
> class Meta:
> model = Model_1
> # etc
>
> That ought to do it...
>
> E
>
> On Jun 3, 2008, at 3:02 PM, Mayank Dhingra wrote:
>
>
>
> > Actually I wanted to know how to
>
> > Say I have a model "Model_1" and I made a form from that model using
> >ModelForm
> > now how am I supposed to addcustomCSSto the fields of this model?
>
> > class Model_1Form(ModelForm):
> > class Meta:
> > model = Model_1
> > exclude = ('hash')
> > def save(self, request):
> > something something
>
> > Let me know if I missed something.
> > Thanks
>
> > On Jun 3, 9:14 am, Eric Abrahamsen <gir...@gmail.com> wrote:
> >> Hey there,
>
> >> You probably want this:http://www.djangoproject.com/documentation/newforms/#customizing-widg
> >> ...
>
> >> You'll have to specify the field types for your model, then widget
> >> types for each field, and the extra attributes go in the widget.
>
> >> Yrs,
> >> E
>
> >> On Jun 3, 2008, at 2:23 AM, Mayank Dhingra wrote:
>
> >>> Hi,
>
> >>> I was looking to addcustomcssto the fields of a (new)form that I
Reply all
Reply to author
Forward
0 new messages