How can I get a textarea from model+ModelForm?


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

But I get a text input not textarea for models.TextField(). Is that a reason of css?

Asked By: shibly



I think this section in the documentation should be useful to solve the problem.

from django.forms import ModelForm, Textarea

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

Alternative to jcollardo’s solution (same result, different syntax):

from django import forms

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

You are using models not forms, which means you can’t use textarea properly. Instead you can try TextField:

field_name = models.TextField( **options)
Answered By: Aldiyar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.