简体   繁体   中英

How to allow POST method with Flask?

I'm doing a web Flask app with "sign up" and "log in" functions. I'd like to use a POST method for the "sign up" function, like this:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Index"

@app.route("/signup/<username>,<password>" , methods = ['POST'])
def signup(username, password):

    return "Hello %s ! Your pw is %s" % (username,password)


if __name__== "__main__":
    app.run(debug=True)

But when I ran it, I received this error : "Method not allowed. The method is not allowed for the requested URL."

How can I do? Thanks in advance.

You could use something like this perhaps.

from flask import Flask, request, render_template, url_for, redirect

...

@app.route("/signup/<username>,<password>", methods = ['GET', 'POST'])
def signup(username, password):
    if request.method == 'POST':
        # Enter your function POST behavior here
        return redirect(url_for('mainmenu'))    # I'm just making this up
    else:
        return "Hello %s ! Your pw is %s" % (username,password)
        # Or you could try using render_template, which is really handy
        # return render_template('signupcomplete.html')

You'd have to flesh it out with the various things you need it to do, but that basic structure should be what you need. I've used it myself in some projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM