django forms中widget属性的自定义

如有model:
代码:
class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()

在django下可以用以下方式生成forms
代码:
from django.forms import ModelForm
class BookForm(ModelForm):
    class Meta:
        model = Book

这时由生成html中title input中没有size属性,是默认大小,如果自定义大小可以如下:
代码:
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


posted @ 2009-11-06 10:02  kid的笔记本  阅读(5765)  评论(0编辑  收藏  举报