简体   繁体   English

Django动态表单字段生成

[英]Django dynamic form field generation

I want to create the form with 'rooms' field, it is integer field. 我想用“房间”字段创建表单,它是整数字段。 Depending on numbers of rooms i want to generate two another fields for each room 'extra_adult' and 'extra_children'. 根据房间的数量,我想为每个房间“ extra_adult”和“ extra_children”生成另外两个字段。 And depending on numbers of children i want to generate 'children_age' fields. 根据孩子的数量,我想生成“ children_age”字段。

I am new to Python and Django and I tried to do like on DYNAMIC FORM GENERATION post and like here but i stuck with several question: 我是Python和Django的新手,我试图在DYNAMIC FORM GENERATION帖子上做类似的文章,但在这里遇到了几个问题:

1) i have an error global name 'rooms' is not defined in forms.py; 1)我在forms.py中global name 'rooms' is not defined时出错;

2) how should i to cleaned_data for extra_adult , extra_children , extra_children_age fields; 2)如何应cleaned_dataextra_adultextra_childrenextra_children_age字段;

3) depending on numbers of children how to add extra_children_age fields. 3)根据孩子的数量,如何添加extra_children_age字段。

Here is my form.py 这是我的form.py

class RoomsForm(forms.Form):
        rooms = forms.IntegerField(label=(min_value=1 )

        def __init__(self, *args, **kwargs):
                extra_adult = kwargs.pop('extra_adult', 0)
                extra_children = kwargs.pop('extra_children', 0)
                super(HotelForm, self).__init__(*args, **kwargs)
                for room in rooms:
                        self.fields['adult_%s' % room] = forms.IntegerField(min_value=1 )
                        self.fields['children_%s' % room] = forms.IntegerField(label=(required=False )

views.py views.py

def bookingForm(request):
        if request.method == 'POST':
                form = HotelForm(request.POST, extra_a=request.POST.get('extra_adult'), extra_c=request.POST.get('extra_children'))
                if form.is_valid():
                        rooms = form.cleaned_data['rooms']
                        print "form is valid"
        else:
                form = HotelForm()

        return render(request, 'booking/booking.html', { 'form': form })

Thank you for your help. 谢谢您的帮助。

Your code needs a lot of improvements, I couldn't write the whole thing for you, but here are some places that seems wrong obviously: 您的代码需要大量改进,我无法为您编写全部内容,但是显然有些地方似乎是错误的:

  1. Your form field definition seems weird: rooms = forms.IntegerField(label=(min_value=1 ) , take a look at some examples to fix it. 您的表单字段定义似乎很奇怪: rooms = forms.IntegerField(label=(min_value=1 ) ,请看一些修复它的示例。

  2. Your first error is because you don't have rooms variable defined within your constructor. 您的第一个错误是因为没有在构造函数中定义rooms变量。 Also, you can't just get value from the field rooms like that, all form fields are filled and submitted at the same time, there's no way that fields could cross refer to each other. 同样,您不能仅从这样的现场rooms获得价值,所有表单字段都同时填写和提交,因此字段之间无法相互引用。 You might need a separate form to just get the room information. 您可能需要单独的表格才能获取房间信息。

  3. You try to do kwargs.pop() , but you feed your form with extra data in the wrong way. 您尝试执行kwargs.pop() ,但是您以错误的方式向表单提供了额外的数据。 To make it work, you should do something like: 要使其正常工作,您应该执行以下操作:

form = HotelForm(request.POST, extra_adult=1, extra_children=2)

The example you see is a little mis-leading, you shouldn't feed those parameters with request.POST.get data, they should be fed with some other sources, that's why I suggest you making the room info in a different form. 您看到的示例有点误导性,不应将那些参数与request.POST.get数据一起提供,而应将它们与其他一些来源一起提供,这就是为什么我建议您以其他形式提供房间信息的原因。 Once you have that, then feed the result to the second form is much easier. 一旦有了,将结果输入第二种形式就容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM