django “ModelFormMetaclass”类型的参数不可迭代

wh6knrhe  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(79)

我是Django的初学者。当我在www.example.com中连接我的类veiws.py,我得到了一个错误,我真的不明白,但当我不连接它时,它与类一起工作
这是我的views.py

class AudioCreateView(LoginRequiredMixin, CreateView):
    login_url = 'main:login'
    model = Audio
    fields = AudioForm
    # fields = ['title','audio','author', 'categories']
    template_name = 'main/events/create_audio.html'

字符串
这是我的forms.py

class AudioForm(ModelForm):

    class Meta:
        model = Audio
        fields = ['id','title','audio','author', 'categories']
        # widgets = {
        #     'audio': forms.FileField(widget=forms.FileInput(attrs={'accept':'application/pdf'}))
        #


请,这是我得到的错误,当我尝试使用它

File "C:\Users\Joe\Desktop\MYDJAN~1\PRIEST~1\env\lib\site-packages\django\forms\models.py", line 551, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
File "C:\Users\Joe\Desktop\MYDJAN~1\PRIEST~1\env\lib\site-packages\django\forms\models.py", line 256, in __new__
    apply_limit_choices_to=False,
File "C:\Users\Joe\Desktop\MYDJAN~1\PRIEST~1\env\lib\site-packages\django\forms\models.py", line 152, in fields_for_model
    if fields is not None and f.name not in fields:
TypeError: argument of type 'ModelFormMetaclass' is not iterable


谁能帮我我不明白为什么我得到这个错误

kadbb459

kadbb4591#

我不太明白你的问题。但在CreateView中,需要指定如下字段:

fields = ['id','title','audio','author', 'categories']

字符串
而且在你的widget行中,你似乎错过了一个结尾,即。它应该是:

widgets = {
        'audio': forms.FileField(widget=forms.FileInput(attrs={'accept':'application/pdf'}))
           }


我不知道这是你的问题,还是只是一个复制和粘贴错误?

wrrgggsh

wrrgggsh2#

我认为问题在于你在CreateView中传递了什么。因为您希望使用Modelform,所以可以使用form_class属性指定表单。

class AudioCreateView(LoginRequiredMixin, CreateView):
    login_url = 'main:login'
    model = Audio
    #Specify you form there, the actual AudioForm define the fields to use
    form_class = AudioForm 
    template_name = 'main/events/create_audio.html'

字符串

相关问题