首页 > 解决方案 > 烧瓶 - 如何发送数据并将其取回

问题描述

我正在烧瓶中开发一个小应用程序,我需要让用户一一回答 20 个随机问题。我有一个问题列表,我想在每次用户按下“下一步”按钮时向 html 发送一个问题。

    @app.route('/handleQuestions', methods=['GET', 'POST'])
    def handleQuestions():
        #first time - first question
        if not request.method == 'POST':
            try:
                pdQuestions = GetShuffeledQuestions()
                session['pdQuestions'] = pdQuestions
                i=0
                session['qindex']=i
                questionTosend = session.get('pdQuestions').iloc[[i]]['question'].item()
                print(questionTosend)
                return  render_template('questionsTemplate.html',progress=round((i/24)*100), sent=questionTosend)
            except Exception as ex:
                print(ex)
        else:
            answer = request.form.to_dict(flat=False)       
            print(answer)
            #next question
            i=session['qindex']+1
            session['qindex']=i
            questionTosend = session.get('pdQuestions').iloc[[i]]['question'].item()
            print(questionTosend)
            return  render_template('questionsTemplate.html',progress=round((i/24)*100), sent=questionTosend)
            print('done')

我在哪里可以保存清单?以及如何将其迭代到页面?我希望用户每次得到 1 个问题,我会保存答案并给他下一个问题。

我试图将其保存在会话中,但列表太大,当我将其保存到会话时它会崩溃。

标签: pythonsessionwebcachingflask

解决方案


推荐阅读