如何从model + ModelForm中获取textarea?

时间:2012-01-06 16:32:59

标签: python django forms model

models.py =>

from django.db import models
from django.forms import ModelForm
from datetime import date
import datetime
from django import forms
from django.forms import Textarea

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    created = models.DateField(auto_now_add=True)
    modified = models.DateField(auto_now_add=True)

    def __unicode__(self):
        return self.title

class PostModelForm(ModelForm):
    class Meta:
        model = Post

但我得到的文字输入不是models.TextField()的textarea。这是css的原因吗?

2 个答案:

答案 0 :(得分:19)

我认为文档中的section应该对解决问题很有用。

from django.forms import ModelForm, Textarea

class PostModelForm(ModelForm):
    class Meta:
        model = Post
        widgets = {
            'content': Textarea(attrs={'cols': 80, 'rows': 20}),
        }

答案 1 :(得分:8)

替代jcollardo的解决方案(结果相同,语法不同):

from django import forms

class PostModelForm(forms.ModelForm):
    content = forms.CharField(widget=forms.Textarea)
    class Meta:
        model = Post