How to add class attribute in Forms modelform Django 1.3


Usually we use forms.py and create class Form to generate form and pass it into views.py. As usual, we need to customize form in ModelForm for instance, we need to add class attribute. So, we can use Widget to solve this problem. Here are how to customize and add class attribute into form Django :

forms.py

1
2
3
4
from django import forms

class YahooParse(forms.Form):
    url = forms.URLField(widget=forms.TextInput(attrs={‘class’: ‘xlarge’}))

This will generate form like this :

1
<input type="text" name="url" class="xlarge" id="id_url">

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.