Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a textarea from model+ModelForm?

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

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

like image 214
shibly Avatar asked Jan 06 '12 16:01

shibly


People also ask

Which code will give us a textarea form field in Django?

CharField() is a Django form field that takes in a text input. It has the default widget TextInput, the equivalent of rendering the HTML code <input type="text" ...> .

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is forms ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

What is model form?

What are Model Forms? Model Forms are forms that are connected directly to models, allowing them to populate the form with data. It allows you to create a form from a pre-existing model. You add an inline class called Meta, which provides information connecting the model to the form.


2 Answers

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}),
        }
like image 60
jcollado Avatar answered Oct 05 '22 23:10

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
like image 33
Tom Avatar answered Oct 05 '22 23:10

Tom