1

I'm developing an online questionnaire by using Flask (python2.7). I want to impede the participants to the questionnaire to navigate back to previous pages. The order of some pages to be shown is random, and the back navigation may impede the completion of the entire questionnaire (a "next page" may be popped out from a list but then the participant goes back and is not able to access that "next page" anymore).

routes = {1:'scenario1', 2:'scenario2', 3:'scenario3', 4:'scenario4', 5:'scenario5', 6:'scenario6', 7:'evaluation'}


@app.route('/', methods=['GET', 'POST'])
def login():
    sForm = StartForm()
    if sForm.validate_on_submit():
        session['ID'] = escape(sForm.username.data)
        return redirect(url_for('photos'))
    return render_template('login.html', form=sForm)

@app.route('/photos', methods=['GET', 'POST'])
def photos():
    nextF = NextForm()
    if nextF.validate_on_submit():
        pages = range(1,7)
        random.shuffle(pages)
        session['pages'] = [7]
        session['pages'].extend(pages)
        return redirect(url_for(routes[session['pages'].pop()]))
    return render_template('photos.html', title='Part II', form=nextF)


@app.route('/scenarioX', methods=['GET', 'POST']) # X in [1,6]
def scenarioX():
    scenFx = ScenarioXForm()
    if scenFx.validate_on_submit():
        return redirect(url_for(routes[session['pages'].pop()]))
    return render_template('scenarioX.html', form=scenFx)


# Ensure responses aren't cached
#@app.after_request
#def after_request(response):
#    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
#    response.headers.add('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
#    return response

#@app.after_request
#def add_header(r):
#  """
#    Add headers to both force latest IE rendering engine or Chrome Frame,
#    and also to cache the rendered page for 10 minutes.
#    """
#    r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
#    r.headers["Pragma"] = "no-cache"
#    r.headers["Expires"] = "0"
#    return r

#@app.after_request
#def add_header(response):
#    response.cache_control.no_store = True
#    if 'Cache-Control' not in response.headers:
#        response.headers['Cache-Control'] = 'no-store'
#    return response

By reading around, I think I need to disable the cache (Disabling caching in Flask and Flask- Back Button Returns to Session Even after Logout), but I haven't found a solution to my problem yet (in the commented part of the code above is what I've tried after reading previous posts).

Francesca
  • 11
  • 2

0 Answers0