2013-04-22 53 views
0

// // MyTemplate的Django的保存未選中的複選框

<tr> 
     <td><input type="text" name="language1" id="language1" /></td> 
     <td><input type="checkbox" name="read1" id="read1" value="yes" /></td> 
     <td><input type="checkbox" name="write1" id="write1" value="yes" /></td> 
     <td><input type="checkbox" name="speak1" id="speak1" value="yes" /></td> 
    </tr> 
    <tr> 
     <td><input type="text" name="language1" id="language1" /></td> 
     <td><input type="checkbox" name="read1" id="read1" value="yes" /></td> 
     <td><input type="checkbox" name="write1" id="write1" value="yes" /></td> 
     <td><input type="checkbox" name="speak1" id="speak1" value="yes" /></td> 
    </tr> 

我想是保存到我的數據庫,如果複選框被選中的值,

如果該複選框選中了,我想保存沒有數據庫,..

如何做到這一點?

編輯

def edit_other_info(request): 
    if request.method == "POST": 

     user = request.POST.get('user_id') 

     language1 = request.POST.get('language1') 
     read1 = request.POST.get('read1') 
     write1 = request.POST.get('write1') 
     speak1 = request.POST.get('speak1') 

     language2 = request.POST.get('language2') 
     read2 = request.POST.get('read2') 
     write2 = request.POST.get('write2') 
     speak2 = request.POST.get('speak2') 
     p=language(user_id=user,language1=language1,read1=read1,write1=write1, 
     speak1=speak1,language2=language2,read2=read2,write2=write2,speak2=speak2) 
     p.save() 

     return HttpResponseRedirect('/accounts/Profile/') 
    else: 

     details1 = language.objects.filter(user_id=request.user.id) 
     return render_to_response('registration/language.html', {"details":details, "details1":details1}, context_instance=RequestContext(request)) 

在此先感謝..

+0

你已經完成了一些Python代碼?如果是這樣,我也很樂意看到! – jsalonen 2013-04-22 12:23:53

回答

3

測試此複選框ID是在後的參數,像這樣:

def myview(request): 
    if request.method == 'POST': 
     read1_val = "read1" in request.POST 
     MyModel(read1=read1_val).save() 

如果是在那裏它是「是」。如果它不在那裏,那就是「不」。

更多hereherehere

3

如果你提供了一個共同的名字給所有的複選框,你可以retrive使用

options = request.POST.getlist('name') 

如果沒有被選中,它返回一個空列表值。