Django 没有属性 'HiddenInput'

Django has no Attribute 'HiddenInput'

我在以下代码中收到错误 module 'django.forms.forms' has no attribute 'HiddenInput'

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()}

有人知道是什么原因造成的吗?我查了下文档,HiddenInput()应该是有效的(https://docs.djangoproject.com/en/2.0/ref/forms/widgets/).

您的导入不正确,将导入更改为:

from django import forms

并更新表单的基础 class:

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

继承 forms.ModelForm

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