Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

html - How to get unchecked checkbox using Flask? (Or something!)

Apparently several people have the same problem.

Unfortunately, they all use PHP which apparently does some weird stuff.

I have some server code that looks like this:

@app.route("/place", methods=['GET', 'POST'])
def place():
    names = request.form.getlist('name')
    checks = request.form.getlist('checkboxes')
    if request.form.get('Add Element'):
        #return template with another form element and all the data
    #return default template with N copies of the input

Now here's the problem - if I use radio buttons for my Yes/No (checked or not), I can't use getlist, because they've all got the same name. Alternatively, I can't use checkboxes, because for some reason "they" decided that checkboxes shouldn't be sent with a "false" value.

This solution doesn't work:

<input id='testName' type='checkbox' value='Yes' name='testName'>
<input id='testNameHidden'  type='hidden' value='No' name='testName'>

Because, then I have somewhere between 1-2x the number of elements I want.

I don't want to use Javascript, though I easily could - this will be an extremely low-use site (maybe 200-300 page views per month, tops, usually clustered together towards the end of month).

One possible solution I had was to use an `YesNo' - but this seems a bit on the clunky side. Unfortunately, I can't readily think of any other way (without javascript involved) to do what I need.

Am I stuck with the select option?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An unchecked checkbox doesn't get serialized into a response - if you are dealing with a case where you have multiple form sections each with a checkbox (for example):

Repeating section #N
    Text field: __________
    Select field: -----------^
    Checkbox: []

Repeating section #N+1
    etc ...

then you can do one of two things:

  • Give each section its own unique prefix or postfix - then you can check the existence of that particular value in the response:

    if request.form.get("checkboxField#N", False):
        # Do something because the box is checked
    
  • Use ImmutableOrderedMultiDict instead of ImmutableMultiDict for your form container (by setting app.request_class to a class with its parameter_storage_class set to an ImmutableOrderedMultiDict):

    class OrderedParamContainer(flask.Request):
         parameter_storage_class = ImmutableOrderedMultiDict
    
    # some time later
    app.request_class = OrderedParamContainer
    

    This will enable you to iterate over the keys and values of the form in the order they were provided to you by the browser (which is the same in all existing cases as the order the form elements are provided to the browser in the source code):

     def by_section(form):
         section = {}
         for key, value in enumerate(form):
             if key == "firstFieldName":
                 yield section
                 section = {}
             section[key] = value
    
         if section: yield section
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...