Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - HTML variable value is not changing in Flask

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...