2013-03-20 53 views
1

如何在視圖內的複選框中循環顯示值。在視圖中迭代逗號分隔列表

這是我到目前爲止已經試過:

if request.method == 'POST': 
     form = SomeForm(request.POST) 
     # Check we have valid data before saving trying to save. 
     if form.is_valid(): 
      data = form.cleaned_data 
      groups = data['thischeckbox'] 
      for item in groups: 
       print(item) 

但是,這給出了一個KeyError異常

+1

嘗試:'組= data.getlist( 'thischeckbox',[])' – aemdy 2013-03-20 16:23:08

+0

,讓我 '字典' 對象有沒有屬性 '的GetList' – Prometheus 2013-03-20 16:34:29

回答

1
if request.method == 'POST': 
    form = SomeForm(request.POST) 
    # Check we have valid data before saving trying to save. 
    if form.is_valid(): 
     data = form.cleaned_data 
     groups = data['thischeckbox'].split(",") 
     for item in range(len(groups)) 
      print(groups[item]) 
+0

謝謝凱瑟琳 – Prometheus 2013-03-20 16:56:24

+0

歡迎:) .... – catherine 2013-03-20 17:04:31