我試圖發送我的序列化表單數據到Django它應該存儲在「mylist」列表「[視頻,音頻]」。無論我在表單上檢查什麼框,views.py都會返回0。django getlist()from ajax html form
HTML表單
<form id="myform" method="POST">
<input type="checkbox" name="list" value="audio"/> Audio<br />
<input type="checkbox" name="list" value="video"/> Video<br />
<input type="submit" value="Get Custom Library!" />
</form>
jQuery的AJAX
<script language="JavaScript">
$(document).ready(function() {
$("#myform").submit(function() {
$.ajax({
type: "POST",
url: 'django/builder/buildit',
data: $(this).serialize(),
success: function(response){
alert(response);
}
});
return false;
});
});
</script>
views.py
from django.http import HttpResponse
def main(request):
mylist = request.POST.getlist('list')
message = 0
for item in mylist:
if item == 'video':
message = 'vid'
elif item == 'audio':
message = 'aud'
return HttpResponse(message)
'數據:{列表:myCheckboxes}' –
嘗試插入'打印(項目)'在views.py找出其實際價值? – Junuxx
爲什麼不使用'mylist'中的'if'視頻而不是循環? – VisioN