1. 程式人生 > >django中form的CSS樣式

django中form的CSS樣式

On a real web page, you probably don't want every widget to look the same. You might want a larger input element for the comment, and you might want the 'name' widget to have some special CSS class. To do this, you use the attrs argument when creating the widget:

Widget.attrs

For example:

class CommentForm(forms.Form):

name = forms.CharField(

widget=forms.TextInput(attrs={'class':'special'}))

url = forms.URLField()

comment = forms.CharField(

widget=forms.TextInput(attrs={'size':'40'}))

Django will then include the extra attributes in the rendered output:

>>> f = CommentForm(auto_id=False)

>>> f.as_table()

<tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>

<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>

<tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>