Python web2py按钮应该做不同的事情

Python web2py按钮应该做不同的事情,python,beautifulsoup,web2py,Python,Beautifulsoup,Web2py,我对web2py和一般网站都是新手。 我想上传包含不同数量问题的xml文件。 我使用bs4解析上传的文件,然后我想做不同的事情:如果xml文件中只有一个问题,我想去一个新站点,如果其中有更多问题,我想去另一个站点。 这是我的代码: def import_file(): form = SQLFORM.factory(Field('file','upload', requires = IS_NOT_EMPTY(), uploadfolder = os.path.join(request.folder

我对web2py和一般网站都是新手。 我想上传包含不同数量问题的xml文件。 我使用bs4解析上传的文件,然后我想做不同的事情:如果xml文件中只有一个问题,我想去一个新站点,如果其中有更多问题,我想去另一个站点。 这是我的代码:

def import_file():
form = SQLFORM.factory(Field('file','upload', requires = IS_NOT_EMPTY(), uploadfolder = os.path.join(request.folder, 'uploads'), label='file:'))
if form.process().accepted:
    soup = BeautifulSoup('file', 'html.parser')
    questions = soup.find_all(lambda tag:tag.name == "question" and tag["type"] != "category")
    # now I want to check the length of the list to redirect to the different URL's, but it doesn't work, len(questions) is 0.
    if len(questions) == 1:
        redirect(URL('import_questions'))
    elif len(questions) > 1:
        redirect(URL('checkboxes'))
return dict(form=form, message=T("Please upload the file"))

上传xml文件后,有人知道我可以做什么来检查列表的长度吗?

beautifulsou
需要字符串或类似文件的对象,但您正在传递
'file'
。相反,您应该使用:

with open(form.vars.file) as f:
    soup = BeautifulSoup(f, 'html.parser')
但是,这不是web2py特有的问题

希望这有帮助