forms - Web2py multi-digit value from dropdown gets treated as a list of values -


i have sqlform.factory field:

field('course', requires=is_in_set(course_query, multiple=true), widget=sqlform.widgets.multiple.widget), 

that gets contents query:

course_query = external_db.executesql("select course_id, course_title course") 

i want user able select 1 or more courses, can. upon submit, course_id(s) submitted captured by:

courses = request.vars.course 

then loop through returned course_id's , insert them table:

    if form.process().accepted:     course in courses:         external_db.enrolment.insert(student_id=student, course_id=course)     response.flash = 'record saved' 

this works fine when user selects more 1 course. each submitted record gets inserted database correct course_id. if user selects 1 course, happens have 2-digit id, first digit gets inserted.

i have found if single 2-digit course_id value submitted web2py treats list, each digit individual element.

how make treat double-digit values request.vars single value instead of list of values?

thanks all.

the integer values returned browser strings, , when have single value selected, end single string rather list of strings. in python, when iterate on string, iterate on individual characters in string. in case, double-digit value two-character string, for loop run once each digit.

a simple solution use form.vars.course instead of request.vars.course. former list when single value has been selected, iterating on list containing single item rather iterating on two-character string.

note, form.vars not populated processed form values until after have called form.process(), may need adjust order of code.

as aside, there no need specify widget argument, widget automatically virtue of using is_in_set(..., multiple=true) validator.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -