smurf
smurf

Reputation: 281

Form validation and MultiValueDictKeyError

Django framework. Html form. views.py script.

I have a form with multiple checkboxes, the user chooses a checkbox in correspondence with what files they would like to download in a zip file. When all boxes are checked, a zip file is created and able to download however whenever a checkbox is unchecked I get the following error:

MultiValueDictKeyError at /qatools/debug/logs/ Key 'logs' not found in QueryDict:

My validation is as follows. It is determines whether a checkbox has been checked and assigns either a 0 or 1 as it is in the format that it calls a shell script.

    logs = 0
    if request.POST.get & request.POST['logs'] == "on":
        logs = 1

Upvotes: 2

Views: 2008

Answers (1)

Rob Osborne
Rob Osborne

Reputation: 4997

This is a standard issue with HTML forms ... on the POST an unchecked checkbox will NOT EXIST in the POST values, not have a value of 'off' as expected. So you need to check for existence:

logs = 0
if request.POST.get and 'logs' in request.POST and request.POST['logs'] == "on":
    logs = 1

Upvotes: 3

Related Questions