request.form["name"]例如,当我键入要从POST提交的表单中检索名称时,是否还必须编写一个类似于以下内容的单独分支request.form.get["name"]?如果我想同时支持这两种方法,是否需要为所有POST和所有GET请求编写单独的语句?
request.form["name"]
request.form.get["name"]
@app.route("/register", methods=["GET", "POST"]) def register(): """Register user."""
你可以使用区分实际方法request.method。
request.method
我假设你要:
import flask app = flask.Flask('your_flask_env') @app.route('/register', methods=['GET', 'POST']) def register(): if flask.request.method == 'POST': username = flask.request.values.get('user') # Your form's password = flask.request.values.get('pass') # input names your_register_routine(username, password) else: # You probably don't have args at this route with GET # method, but if you do, you can access them like so: yourarg = flask.request.args.get('argname') your_register_template_rendering(yourarg)