Web2py 比较 request.vars 元素的一部分 [英] Web2py comparing part of a request.vars element
问题描述
我有一个表格,表格中的行包含带有 _names 和 ID 的 SELECT,如下所示:
I have a form with a table with rows containing SELECTs with _names with IDs attached, like this:
TD_list.append(TD(SELECT(lesson_reg_list, _name='lesson_reg_' + str(student[4]))))
提交表单时,我想提取 student[4] 值 和 request.vars.lesson_reg_student[4] 持有的值.
When the form is submitted I want to extract both the student[4] value and the value held by request.vars.lesson_reg_student[4].
我尝试过类似的方法:
for item in request.vars:
if item[0:9] == "lesson_reg":
enrolment_id = int(item[10:])
code = request.vars.item
我还尝试使用以下方法将 request.vars 视为字典:
I also tried treating request.vars like a dictionary by using:
for key, value in request.vars:
if key[0:9] == "lesson_reg":
enrolment_id = int(key[10:])
code = value
但后来我得到了太多的价值无法解压".如果 request.vars 项目名称的最后一部分可以是任意数字,再加上项目名称本身的子字符串,我该如何检索该项目的值?
but then I got 'too many values to unpack'. How do I retrieve the value of a request.vars item when the last part of its name could be any number, plus a substring of the item name itself?
预先感谢您帮助我.
推荐答案
在 Python 中,切片时,结束索引是排除,所以你的两个切片应该是 0:10code> 和
0:11
.为简化起见,您还可以将 .startswith
用于第一个:
In Python, when slicing, the ending index is excluded, so your two slices should be 0:10
and 0:11
. To simplify, you can also use .startswith
for the first one:
for item in request.vars:
if item.startswith('lesson_reg'):
enrolment_id = int(item[11:])
code = request.vars.item
这篇关于Web2py 比较 request.vars 元素的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!