代码:
在django下可以用以下方式生成forms
class 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
![](http://img.zemanta.com/pixy.gif?x-id=b4db6b2f-1e9a-88a0-a8fa-2aeb142318b1)