【问题标题】:Django 'TestForm' object has no attribute 'fields'Django 'TestForm' 对象没有属性 'fields'
【发布时间】:2018-10-07 02:09:55
【问题描述】:

我正在使用 django:

我正在尝试将元组列表从 views.py 传递到下拉框表单,但我收到此属性错误

forms.py

import logging                                                                   

from django import forms                                                         

log = logging.getLogger(__name__)                                                

class TestForm(forms.Form):                                                    

    def __init__(self, *args, **kwargs):                                         
        testlist = kwargs.pop('testlist',None)                               
        log.info(regionlist)                                                     
        self.fields['testlist'] = forms.ChoiceField(choices=testlist)        
        super(TestForm, self).__init__(*args, **kwargs) 

views.py

form = forms.RegionForm(regionlist=data)     

我是否使用正确的方法在views.pyforms.py 之间传递变量?

【问题讨论】:

    标签: python django forms dropdownbox


    【解决方案1】:

    您需要先调用super,以便超类设置fields 属性。

    def __init__(self, *args, **kwargs):                                         
        testlist = kwargs.pop('testlist', None)
        log.info(regionlist)
        super(TestForm, self).__init__(*args, **kwargs)
        self.fields['testlist'] = forms.ChoiceField(choices=testlist)        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 2015-02-19
      • 2016-03-30
      • 2021-04-05
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多