【问题标题】:Render django.forms.fields.ChoiceField object渲染 django.forms.fields.ChoiceField 对象
【发布时间】:2020-03-19 12:40:25
【问题描述】:

在我的 Django 项目中,我遇到了以下问题: 我想要一个动态的 Django 表单。在第一步中,第一个表单向用户询问某些内容。当我得到 postmethod 时,变量应该用于生成新表单

我的意见.py

def calc(request):
    if request.method =="POST":
        get_form = CalculationForm(request.POST)
        if get_form.is_valid():
            op = get_form.cleaned_data['op']
            ab = get_form.cleaned_data['ab']
            alternative = AlternativForm(optype = op, wsgroup = ab)
            return render(request, 'calculated_lensar.html', {"alternativ" : alternativ})

    else:
        form = CalculationForm()
        return render(request, 'calc.html', {'form': form})

第二种形式(postmethod)看起来像

class AlternativForm(forms.Form):

    praep_button = ((3, 'hallo'), (4, 'tschüss'))

    def __init__(self, optype, wsgroup, *args, **kwargs):
        super(AlternativForm, self).__init__(*args, **kwargs) #dont know for what this is standing
        self.optype = optype
        self.wsgroup = wsgroup
        self.values = self.read_db()
        self.praep_button = self.buttons()
        self.felder = self.blub()
        self.neu2 = self.myfield_choices()


    def read_db(self):
        import sqlite3
        ....
        return result #tuple with 15x5 elements

    def buttons(self):
        praep_button = []
        for i in self.values:
            praep_button.append((i[4], i[1]))
        return praep_button #Just formating result from read_db in tuple(15x2)

    def blub(self):
        return forms.ChoiceField(widget=forms.RadioSelect, choices=self.praep_button)

    myfield = forms.ChoiceField(widget=forms.RadioSelect, choices=praep_button) #print --><django.forms.fields.ChoiceField object at 0x751f9b90>

    def myfield_choices(self):

        field = self['myfield'] 


    """i think here is the problem. 
    Above 'myfield' is a django.forms.fields.ChoiceField object, but here it is rendered to html (like it should be). I have the code from https://stackoverflow.com/questions/6766994/in-a-django-form-how-do-i-render-a-radio-button-so-that-the-choices-are-separat. 
    But instead i should use field = self.felder (radioselect woth tuple of the db)"""


        widget = field.field.widget

        attrs = {}
        auto_id = field.auto_id
        if auto_id and 'id' not in widget.attrs:
            attrs['id'] = auto_id
        name = field.html_name

        return widget.render(name, field.value(), attrs=attrs)
        #return widget.get_renderer(name, field.value(), attrs=attrs)

总而言之,我希望问题很清楚。 如果我使用 AlternativForm() 我得到常量形式。相反,我想获得一个动态表格。如果我在views.py中访问:

 alternative = AlternativForm(optype = op, wsgroup = ab)
 alternative = alternativ.felder

比我得到的。我可以把它渲染成 html 吗?

如果我在forms.py中设置:

 field = self.felder

我得到的错误是它是一个字段而不是一个小部件

感谢您的阅读!

【问题讨论】:

    标签: django django-forms radio-button python-3.7


    【解决方案1】:

    您只需要在表单的__init__() 方法中分配选项。几乎你正在做的事情,但不是将self.felder 定义为一个字段,而是需要使用已经初始化的表单字段:

    myfield = forms.ChoiceField(widget=forms.RadioSelect, choices=praep_button)
    
    def __init__(self, optype, wsgroup, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['myfield'].choices = self.get_choices(optype, wsgroup)  # create your choices in this method
    
    def get_choices(optype, wsgroup):
        # call your other methods here
        return praep_button
    

    【讨论】:

    • 非常感谢。这非常迅速而且很有帮助!
    猜你喜欢
    • 2018-06-29
    • 2019-08-11
    • 2013-07-05
    • 1970-01-01
    • 2011-09-18
    • 2021-05-01
    • 2018-01-27
    • 2019-11-15
    • 2021-09-03
    相关资源
    最近更新 更多