CSS styling in Django forms -


how style following:

in forms.py --

from django import forms  class contactform(forms.form):     subject = forms.charfield(max_length=100)     email = forms.emailfield(required=false)     message = forms.charfield(widget=forms.textarea) 

in contact_form.html --

    <form action="" method="post">         <table>             {{ form.as_table }}         </table>         <input type="submit" value="submit">     </form> 

for example, how set class or id subject, email, message provide external style sheet to? thank you

taken answer to: how markup form fields <div class='field_type'> in django

class myform(forms.form):     myfield = forms.charfield(widget=forms.textinput(attrs={'class' : 'myfieldclass'})) 

or

class myform(forms.modelform):     class meta:         model = mymodel      def __init__(self, *args, **kwargs):         super(myform, self).__init__(*args, **kwargs)         self.fields['myfield'].widget.attrs.update({'class' : 'myfieldclass'}) 

or

class myform(forms.modelform):     class meta:         model = mymodel         widgets = {             'myfield': forms.textinput(attrs={'class': 'myfieldclass'}),         } 

--- edit ---
above easiest change make original question's code accomplishes asked. keeps repeating if reuse form in other places; classes or other attributes work if use django's as_table/as_ul/as_p form methods. if need full control custom rendering, clearly documented

-- edit 2 ---
added newer way specify widget , attrs modelform.


Comments