How to hide variables from flask url routing?

佐手、 提交于 2020-08-10 18:23:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!