English 中文(简体)
Python从列表框中获取所有元素
原标题:Python get all elements from a listbox

我有一个Flask和Python web应用程序,在那里我为两个对象实现了CRUD方法:TeamEmployee;在多对多的关系中。一个员工可以是多个团队的成员,一个团队可以有多个员工。

现在,当我创建一个团队时,我有两个列表框:一个包含所有员工,另一个包含将成为团队一部分的员工。有了两个按钮,我可以将员工从一个列表框移动到另一个,反之亦然。

最后,我可以按下Add按钮在Sqlite3数据库中创建团队,其中有三个表:employee、team、teams_employes。

github.com项目是

HTML表单是github.com上的以下代码

然后我有了一个控制器Python方法像这样。问题是这个调用:

 team_members : request.form.getlist( team_members[] )

仅返回团队成员列表框中的选定员工。我需要的是更改这行代码,以使team_members列表框中的所有员工都独立,无论他们是否被选中。

有人能建议我如何更改代码以实现这种行为吗?

问题回答

表单提交的默认行为是提交每个HTML小部件的。select元素的值是选定的选项。

为了实现您的目标,

  1. 创建一个隐藏的文本输入元素,其值是选择元素的所有值的逗号分隔字符串。您在flask中检索该值(它将以字符串形式出现),然后在“,”上拆分以获得值数组。

  2. 上述方法的另一种选择是,在提交表单时,通过Javascript拦截提交,然后以编程方式执行项目符号1。

更新(添加示例代码)

员工示例列表

employees = [{"id":100, "name":"John James"}, 
    {"id":200, "name":"Mary Kay"}, 
    {"id":300, "name":"Kent Clark"}]

HTML表单

<form method="POST" action="/post-form/">
...
<!-- Get ONLY the names of all employees -->
{% set all_employees =  employees | selectattr("name") | map(attribute="name") | list %}

<input value="{{all_employees}}"  style="display:none;" name="all_employees"/>

<input type="submit" value="Submit">
</form>


FLASK代码

@app.route("/post_form/", methods=["POST"])
def process_form():
    all_employees = request.values.get("all_employees")
    
    # You ll get a list of all employees on the screen
    return(all_employees)





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签