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?
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" ...> .
Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.
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 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.
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}),
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With