English 中文(简体)
火焰中饼干值的递增
原标题:Increment the cookie value in Flask
  • 时间:2021-11-17 11:24:10
  •  标签:
  • python
  • flask
I want to count the number of visits to the page. Below is the code for the same. When I run the code, it doesn t set the visit_count cookie and /visit returns None value. What s the problem? from flask.helpers import make_response app = Flask(__name__) @app.route( / , methods=[ GET , POST ]) def show_page(): return render_template( cookiecounter.html ) @app.route( /setcookie , methods=[ GET , POST ]) def setcookie(): resp = make_response( Welcome to our site! ) resp.set_cookie( user_visit ,str(1), max_age=5*60*60) return resp @app.route( /visit , methods=[ GET , POST ]) def visit(): vc=request.cookies.get( user_visit ) if vc==1: return You are visiting for the first time else: vscount=int(vc)+1 resp1 = make_response( new visit ) resp1.set_cookie( visit_count ,str(vscount), max_age=5*60*60) inc_count=request.cookies.get( visit_count ) return str(inc_count) if __name__=="__main__": app.run(debug=True)
最佳回答
There s a couple things going on that cause the unexpected result. resp1.set_cookie( visit_count ,str(vscount), max_age=5*60*60) When you do this you re not actually setting the cookie s value yet, just preparing the response so that the cookie will be set once the client receives your response. inc_count=request.cookies.get( visit_count ) Since the previous statement did not actually set any cookie, this is effectively retrieving the same value as when you first called it, which is the old value. return str(inc_count) Remember how you set up your response so that it would set a cookie on the client? Since you re not returning that response, the updated cookie is never set and you re left with the old one (which in your case is no cookie at all). Plus you re also returning the old value because you retrieved it in the line before. Another thing is you re checking equality between your variable holding the string "1" and the number 1, that is probably not what you meant to do. I m not sure why you re using two different cookies user_visit and visit_count. from flask.helpers import make_response from flask import Flask, request app = Flask(__name__) @app.route( /visit , methods=[ GET , POST ]) def visit(): vc=request.cookies.get( user_visit , "1") resp = make_response(f"This is visit number {int(vc)}") vscount=int(vc)+1 resp.set_cookie( user_visit ,str(vscount), max_age=5*60*60) return resp This is probably close to what you wanted to achieve. https://docs.python.org/3/library/stdtypes.html#dict.get (dict get) https://www.python.org/dev/peps/pep-0498/ (string interpolation) Happy pythoning
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签