显示具有相同名称的多个选定值

时间:2013-02-05 05:24:15

标签: python django

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

这是我的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>

2 个答案:

答案 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')