如何使用Flask取消选中复选框? (或者其他的东西!) [英] How to get unchecked checkbox using Flask? (Or something!)

查看:127
本文介绍了如何使用Flask取消选中复选框? (或者其他的东西!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然有几个人有相同

不幸的是,他们都使用PHP,显然做了一些奇怪的东西。



  @ app.route(/ place,methods = [' GET','POST'])
def place():
names = request.form.getlist('name')
checks = request.form.getlist('checkboxes')
if request.form.get('Add Element'):
#返回具有另一个表单元素和所有数据的模板
#返回具有N个输入副本的默认模板

现在就是这个问题 - 如果我使用单选按钮来选择是/否(选中或不选),我不能使用 getlist ,因为它们都有相同的名字。或者,我不能使用复选框,因为某些原因,他们决定不应该使用false值发送复选框。



此解决方案不起作用:

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

因此,我需要1-2x的元素数量。

我不想使用Javascript,但我很容易 - 这将是一个非常低使用的网站(也许每月200-300页面浏览量,通常聚集在一起月底)。

一个可能的解决方案是使用'YesNo' - 但是这看起来有点笨重。不幸的是,我不能轻易想到任何其他的方式(不涉及JavaScript)来做我所需要的。



我坚持使用 select 选项?

解决方案

未经检查的复选框不会被序列化为响应 - 如果您正在处理在这种情况下,您有多个表单部分,每个表单部分都带有复选框(例如):

 重复部分#N 
文本字段:__________
选择字段:----------- ^
复选框:[]

重复章节#N + 1
等...

然后您可以执行以下两项操作之一:


  • 为每个部分分配自己的唯一前缀或后缀 - 然后您可以检查响应中是否存在该特定值:

     如果request.form.get(checkboxField#N,False):
    #做一些事情,因为框被选中


  • 使用 ImmutableOrderedMultiDict 而不是 ImmutableMultiDict 为您的表单容器(通过设置 app.request_class 给一个类,它的 parameter_storage_class 设置为 ImmutableOrderedMultiDict ):

      class OrderedParamContainer(flask.Request):
    parameter_storage_class = ImmutableOrderedMultiDict

    #一段时间后
    app.request_class = OrderedParamContainer

    这将使您可以迭代键和值表单按照浏览器提供给您的顺序(在所有现有的情况下,与在源代码中将表单元素提供给浏览器的顺序相同):

    pre $ $ $ c $ def $ by $ section $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ firstFieldName:
    yield部分
    部分= {}
    部分[key] =值

    如果部分:yield部分



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?

解决方案

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
    

这篇关于如何使用Flask取消选中复选框? (或者其他的东西!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆