Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have written this code in Flask

ans = 999
@app.route('/', methods=['POST', 'GET'])
def home():
    flag = 0
    global ans
    session["ans"] = 0
    if (request.method == "POST"):
        jsdata = request.form['data']
        flag = 1
        session['jsdata'] = jsdata
    if (flag == 1):
        ans = get_data(session['jsdata'])
        return render_template('/index.html',ans=ans)
    return render_template('/index.html',ans=ans)

When the value of flag was 0, in index.html it shows 999, but when value of flag changes to 1 and if condition is executed index.html still show value 999 not the values it got from function. and when I print the and in if condition for debugging it shows correct value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

You really don't need flag at all, it is confusing the entire logic:

@app.route('/',methods = ['POST','GET'])
def home():
  if request.method == "POST":
      jsdata = request.form['data']
      session['jsdata']=jsdata
      session['ans'] = get_data(session['jsdata'])

  ans = session.get('ans', 999) # try to get it from the session,
                                # if fails, set it to 999 default value

  return render_template('/index.html', ans=ans)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...