Search code examples
pythondjangoformsdjango-formshidden-field

Django has no Attribute 'HiddenInput'


I am getting the error module 'django.forms.forms' has no attribute 'HiddenInput' on the following code:

from django.forms import ModelForm, forms

from comments.models import Comment


class CommentForm(ModelForm):
    class Meta:
        model = Comment
        fields = ['video', 'text']
        widgets = {'video': forms.HiddenInput(),
                   'text': forms.TextArea()}

Does anyone know what could be causing this? I looked it up in the documentation, and HiddenInput() should be valid (https://docs.djangoproject.com/en/2.0/ref/forms/widgets/).


Solution

  • You have incorrect import, change import to this:

    from django import forms
    

    and update form's base class:

    class CommentForm(forms.ModelForm):
        class Meta:
            model = Comment
            fields = ['video', 'text']
            widgets = {'video': forms.HiddenInput(),
                       'text': forms.Textarea()}