HTML variable value is not changing in Flask

2024/9/20 17:54:51

I have written this code in Flask

ans = 999
@app.route('/', methods=['POST', 'GET'])
def home():flag = 0global anssession["ans"] = 0if (request.method == "POST"):jsdata = request.form['data']flag = 1session['jsdata'] = jsdataif (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.

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']=jsdatasession['ans'] = get_data(session['jsdata'])ans = session.get('ans', 999) # try to get it from the session,# if fails, set it to 999 default valuereturn render_template('/index.html', ans=ans)
https://en.xdnf.cn/q/119303.html

Related Q&A

How to add two lists with the same amount of indexs in python

I am still new to coding so i apologize for the basic question. How do I add to elements of two seperate lists? listOne = [0, 1 , 7, 8] listTwo = [3, 4, 5, 6] listThree = []for i in listOne:listAdd = …

How to crawl thousands of pages using scrapy?

Im looking at crawling thousands of pages and need a solution. Every site has its own html code - they are all unique sites. No clean datafeed or API is available. Im hoping to load the captured data i…

Object Transmission in Python using Pickle [duplicate]

This question already has answers here:Send and receive objects through sockets in Python(3 answers)Closed last year.I have the following class, a Point objectclass Point:def __init__(self):passdef __i…

Google App Engine: Modifying 1000 entities

I have about 1000 user account entities like this:class UserAccount(ndb.Model):email = ndb.StringProperty()Some of these email values contain uppercase letters like [email protected]. I want to select …

more efficient method of dealing with large numbers in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

MLM downline distribution count

I make my first MLM software and I think I managed to code how to get the points from the downline even though it is a recursive problem I didnt use recursion and I might refactor to a recursive versio…

Can someone please explain to me the purpose of the asterisk in Python? [duplicate]

This question already has answers here:What does asterisk * mean in Python? [duplicate](5 answers)How are pythons unpacking operators * and ** used?(1 answer)Closed 5 years ago.For instance, can some…

Linear Programming with cvxpy

I would like to ask you regarding on the Linear Program for optimization.I have an objective function, and constraint functions as below,variables(x1, x2, x3, x4, x5, x6) are quantities of the products…

Program runs forever without giving an error when plotting data only on continent [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

KeyError while perfoming solve of two equation

1st i need to get two equation of two longest line length i put lenghths with eq in list like these [( length 1 , eq 1 ) ,.....] sort list with reverse get two equation of two longest line when run the…