I'm trying to pass a number through URL and retrieve it on another page. If I try to specify the variable type, i get a malformed URL error and it won't compile. If I don't specify the var type, it will run, but the variable becomes Type None. I can't cast it to an int either. How can I pass it as an Integer...? Thanks in advance.
This gives me a malformed URL error:
@app.route('/iLike/<int: num>', methods=['GET','POST'])
def single2(num):
This runs but gives me a var of type none that I can't work with:
@app.route('/iLike/<num>', methods=['GET','POST'])
def single2(num):
try:
location = session.get('location')
transType = session.get('transType')
data = session.get('data')
**num = request.args.get('num')**
You are mixing route parameters and request arguments here.
Parameters you specify in the route are route parameters and are a way to declare variable rules. The values for these parameters are passed as function arguments to the route function. So in your case, for your <num>
url part, the value is passed as the function argument num
.
Request arguments are independent of routes and are passed to URLs as GET parameters. You can access them through the request object. This is what you are doing with request.args.get()
.
A full example would look like this:
@app.route('/iLike/<int:num>')
def single2(num):
print(num, request.args.get('num'))
Opening /iLike/123
would now result in 123 None
. The request argument is empty because you didn’t specify one. You can do that by opening /iLike/123?num=456
, which would result in 123 456
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With