Django没有属性' HiddenInput'

时间:2018-04-13 05:28:28

标签: python django forms django-forms hidden-field

我在以下代码中收到错误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/)。

2 个答案:

答案 0 :(得分:2)

您导入错误,请将导入更改为:

from django import forms

并更新表单的基类:

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

答案 1 :(得分:0)

继承forms.ModelForm

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