我试图在表单中选择多个复选框后显示多个复选框值,但我只能显示最后选择的值而不是多个选定值。如何显示所有选定的值?
这是我的views.py
def multi(request):
if 'hobbies' in request.GET and request.GET['hobbies']:
hob = request.GET.getlist('hobbies')
for h in hob:
message = 'You entered ..... %r' % h
else:
message = 'enterd wrong datails'
return HttpResponse(message)
我的模板是
<html>
<head>
<title>Search</title>
</head>
<body bgcolor='#455772' >
<form action="/multi/" method="get">
Hobbies:<input type='checkbox' name='hobbies' value='football'/>football <input
type='checkbox' name='hobbies' value='cricket'/>cricket<input type='checkbox'
name='hobbies'value='others'/>others<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
答案 0 :(得分:2)
def multi(request):
if 'hobbies' in request.GET and request.GET['hobbies']:
message = ','.join(request.GET.getlist('hobbies'))
else:
message = 'enterd wrong datails'
return HttpResponse(message)
答案 1 :(得分:0)
试试这个
hob = request.GET.getlist(u'hobbies')