问题
I'm making API and looking for a way to hide the extra information from the url. I have a function index:
@app.route('/', methods=['GET', 'POST'])
def index():
count = foo()
return redirect(url_for("result", count=count))
and a function result
@app.route("/done/<count>")
def result(count):
count = count
return jsonify(count=count)
Inner function count allwase return different values. At the end I get a result like
http://127.0.0.1:5000/done/43
But I need more common url view for universal API like
http://127.0.0.1:5000/done
The problem is that if I remove <count> from endpoint, i get error
TypeError: result() missing 1 required positional argument: 'count'
Is there a way to override this?
回答1:
This task solving by session variable
from flask import session
@app.route('/', methods=['GET', 'POST'])
def index():
count = foo()
session['count'] = count
return redirect(url_for("result"))
@app.route("/done/")
def result(count):
count = session['count']
return jsonify(count=count)
来源:https://stackoverflow.com/questions/54984967/how-to-hide-variables-from-flask-url-routing