Django mutliple相同的GET参数名称不起作用

时间:2016-03-05 03:02:14

标签: python html django

我有这个简单的Django表单,用GET形式过滤数据:

from reservations.models import Reservation, ServiceType
from django import forms


PAYMENT_OPTIONS = (
    ('CASH', 'Cash'),
    ('ROOM', 'Charge to room'),
    ('ACCOUNT', 'Account'),
    ('VISA', 'Visa'),
    ('MASTERCARD', 'Mastercard'),
    ('AMEX', 'Amex'))

class FilterForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(FilterForm, self).__init__(*args, **kwargs)
        self.fields['service_date_from'].widget.attrs['class'] = 'datepicker'
        self.fields['service_date_to'].widget.attrs['class'] = 'datepicker'

    service_date_from = forms.CharField()
    service_date_to = forms.CharField()
    payment_options = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                         choices=PAYMENT_OPTIONS)

然后在模板中:

<fieldset>
    <label>{{form.payment_options.label}}</label>
    {{form.payment_options}}
</fieldset>

HTML:

<fieldset>
   <label>Payment options</label>
   <ul id="id_payment_options">
      <li><label for="id_payment_options_0"><input id="id_payment_options_0" name="payment_options" type="checkbox" value="CASH"> Cash</label></li>
      <li><label for="id_payment_options_1"><input id="id_payment_options_1" name="payment_options" type="checkbox" value="ROOM"> Charge to room</label></li>
      <li><label for="id_payment_options_2"><input id="id_payment_options_2" name="payment_options" type="checkbox" value="ACCOUNT"> Account</label></li>
      <li><label for="id_payment_options_3"><input id="id_payment_options_3" name="payment_options" type="checkbox" value="VISA"> Visa</label></li>
      <li><label for="id_payment_options_4"><input id="id_payment_options_4" name="payment_options" type="checkbox" value="MASTERCARD"> Mastercard</label></li>
      <li><label for="id_payment_options_5"><input id="id_payment_options_5" name="payment_options" type="checkbox" value="AMEX"> Amex</label></li>
   </ul>
</fieldset>

问题是,当我选择两个或更多付款选项时,我只会在网址中找到最后一个。

例如,当我选择现金和帐户时,我会得到类似?payment_options=ACCOUNT而不是?payment_options=CASH&payment_options=ACCOUNT

的内容

我该如何解决?我认为payment_options应该是payment_options[],但不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

您的PAYMENT_OPTIONS选择阵列没问题。

这就是我直接从模型

获取付款选项的方法
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super(MyForm, self).__init__(*args, **kwargs)

self.fields['payments'] = forms.ModelMultipleChoiceField(
                                queryset=Payment.objects.all(),
                                required=True,
                                error_messages = {'required': 'Payment Options is Required!'},
                                label='Payment Types',
                                widget=forms.CheckboxSelectMultiple(attrs={
                                  'class': 'checkbox-inline',}))

请注意ModelForm

class MyForm(forms.ModelForm): 

以及ModelMultipleChoiceField

self.fields['payments'] = forms.ModelMultipleChoiceField(

另请注意,我使用POST方法保存结果。