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.
post() method is used to generate a POST request. This method has can contain parameters of URL, params, headers and basic authentication. URL is the location for sending the request. Params are the list of parameters for the request.
Create a simple HTML page to display text. create a route ”/” and return home. html from the function. Then run your api.py file and click on the link that it provides after running.
Flask sends form data to template Flask to send form data to the template we have seen that http method can be specified in the URL rule. Form data received by the trigger function can be collected in the form of a dictionary object and forwarded to the template to render it on the corresponding web page.
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.
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