django forms中widget属性的自定义
如有model:
代码:
在django下可以用以下方式生成formsclass Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
代码:
这时由生成html中title input中没有size属性,是默认大小,如果自定义大小可以如下:
from django.forms import ModelForm
class BookForm(ModelForm):
class Meta:
model = Book
代码:
from django.forms import ModelForm
class BookForm(ModelForm):
class Meta:
model = Book
title=forms.CharField(label=u'标题', widget=forms.TextInput(attrs={'size':'40'}))
from http://www.okpython.com/bbs/thread-3294-1-2.html