2016-03-05 82 views
0

我有過濾數據這個簡單的Django的形式GET形式: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> 

的問題是,當我選擇兩個或更多的支付選擇,我只得到在url中的最後一個。

因此,例如,當我選擇現金和賬戶,我會得到像?payment_options=ACCOUNT,而不是?payment_options=CASH&payment_options=ACCOUNT

我該如何解決呢?我在想,payment_options應該payment_options[]但不知道該怎麼做。

回答

1

PAYMENT_OPTIONS選擇陣列是OK。

這是我如何把它直接從型號

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我以保存結果。

相關問題