Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用烧瓶允许POST方法?_Python_Flask - Fatal编程技术网

Python 如何使用烧瓶允许POST方法?

Python 如何使用烧瓶允许POST方法?,python,flask,Python,Flask,我正在做一个具有“注册”和“登录”功能的web Flask应用程序。我想为“注册”功能使用POST方法,如下所示: from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Index" @app.route("/signup/<username>,<password>" , methods = ['POST']) def signup(usernam

我正在做一个具有“注册”和“登录”功能的web Flask应用程序。我想为“注册”功能使用POST方法,如下所示:

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)
从烧瓶导入烧瓶
app=烧瓶(名称)
@应用程序路径(“/”)
def index():
返回“索引”
@app.route(“/signup/,”,方法=['POST'])
def注册(用户名、密码):
返回“您好%s!您的密码是%s”%(用户名、密码)
如果名称=“\uuuuu main\uuuuuuuu”:
app.run(debug=True)
但是当我运行它时,我收到了这个错误:“不允许使用方法。请求的URL不允许使用该方法。”


我该怎么办?提前谢谢。

也许你可以用这样的东西

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')
从烧瓶导入烧瓶,请求,呈现模板,url,重定向
...
@app.route(“/signup/,”,方法=['GET','POST'])
def注册(用户名、密码):
如果request.method==“POST”:
#在此处输入您的函数POST行为
return redirect(url_for('main menu'))#我只是在编这个
其他:
返回“您好%s!您的密码是%s”%(用户名、密码)
#或者您可以尝试使用render_模板,这非常方便
#返回呈现模板('signupcomplete.html')

你必须用你需要它做的各种事情来充实它,但基本结构应该是你需要的。我自己也在一些项目中使用过它。

您是如何访问URL的?如果你只是在你的浏览器中去那里,那是一个好机会。使用curl您需要
curl-xpost
。我只需进入浏览器并测试我的应用程序。如果我使用PyCharm,如何处理curl?使用curl解决不了任何问题。查看本教程:谢谢你的建议,但现在我对使用HTML表单不感兴趣。这是我想要的唯一方法吗?你需要一个HTML表单。或者JavaScript。谢谢@coralv。我想我不明白我怎么能做一个POST请求。我的目标是从URL中获取用户名和密码,然后将它们保存到数据库中。也许这会对您有所帮助。我用了这门课(它是免费的),它复习了很多关于烧瓶和其他东西的好东西。