2

我试图在表单中选择它们后显示多个复选框值,但我只能显示最后一个选定的值而不是多个选定的值。如何显示所有选定的值?

这是我的意见.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>
4

2 回答 2

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)
于 2013-02-05T06:26:36.963 回答
0

试试这个

hob = request.GET.getlist(u'hobbies')
于 2013-02-05T05:40:38.230 回答