开发者

Form validation and MultiValueDictKeyError

开发者 https://www.devze.com 2023-03-31 14:16 出处:网络
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

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 fou开发者_JAVA技巧nd 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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消