'TextField missing in django.forms
from django import forms
class UserForm(forms.ModelForm):
first_name = forms.TextField(label=_(u'First name'), required=False)
last_name = forms.TextField(label=_(u'Last name'))
The code above gives me an "AttributeError: 'module' object has no attribute 'TextField'. Everything seems to be ok, except the missing TextField:
ipdb> forms
<module 'django.forms' from '/usr/local/lib/python2.7/dist-packages/django/forms/__init__.pyc'>
ipdb> forms.
forms.BaseForm forms.EmailField forms.MultiWidget forms.TypedChoiceField
forms.BaseModelForm forms.Field forms.MultipleChoiceField forms.TypedMultipleChoiceField
forms.BooleanField forms.FileField forms.MultipleHiddenInput forms.URLField
forms.CharField forms.FileInput forms.NullBooleanField forms.ValidationError
forms.CheckboxInput forms.FilePathField forms.NullBooleanSelect forms.Widget
forms.CheckboxSelectMultiple forms.FloatField forms.PasswordInput forms.fields
forms.ChoiceField forms.Form forms.RadioSelect forms.fields_for_model
forms.ClearableFileInput forms.HiddenInput forms.RegexField forms.forms
forms.ComboField forms.IPAddressField forms.Select forms.formsets
forms.DEFAULT_DATETIME_INPUT_FORMATS forms.ImageField forms.SelectMultiple forms.model_to_dict
forms.DEFAULT_DATE_INPUT_FORMATS forms.IntegerField forms.SlugField forms.models
forms.DEFAULT_TIME_INPUT_FORMATS forms.Media forms.SplitDateTimeField forms.save_instance
forms.DateField forms.MediaDefiningClass forms.SplitDateTimeWidget forms.util
forms.DateInput forms.ModelChoiceField forms.TextInput forms.widgets
forms.DateTimeField forms.ModelForm forms.Textarea
forms.DateTimeInput forms.ModelMultipleChoiceField forms.TimeField
forms.DecimalField forms.MultiValueField forms.TimeInput
Any idea?
Solution 1:[1]
If you want a textarea you can use the forms.CharField with the forms.TextArea widget.
class ContactForm(forms.Form):
message = forms.CharField(widget=forms.Textarea)
Solution 2:[2]
Just want to add a better example for beginners like me, as all mentioned above, there is no TextFile for ModelForm, and if you need to use it, for the OP's case, should be:
first_name = forms.CharField(
widget=forms.TextInput(attrs={'placeholder': 'first name'}),
label=_(u'First name'),
required=False)
And if you do need a textarea field for description or comment, then you can use Textarea widget:
description = forms.CharField(
widget=forms.Textarea(attrs={'placeholder': 'Please enter the description'}))
Solution 3:[3]
forms.TextField doesn't exist, check out the documentation you refer to, it's only for the models.
You can prove this to yourself by looking at the actual django source django/forms/fields.py.
You must use CharField with widget=forms.TextArea as you say in your comments.
Solution 4:[4]
Just use forms.Textarea instead of forms.TextInput.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shang Wang |
| Solution 2 | |
| Solution 3 | |
| Solution 4 |
