我有一个带有 2 个独立表单的网站,我希望能够将数据 POST 到两个不同的函数中。让我解释一下我的意思。第一页是“/”。如果用户从该页面提交表单,则会将其发送到 getLoginForm() 函数,但如果用户位于“/control”页面,则会将数据发送到 getControlForm()。它当前所做的就是为它们调用 getLoginForm() 函数。然后立即出现 400 错误。这是我这两个函数的代码。
@app.route('/',methods=['POST'])
def getLoginForm():
username=request.form['username']
pwrd=request.form['password']
#other stuff to do with the username and password. I've made it return the username just for example purposes.
return username
和
@app.route('/control',methods=['POST'])
def getControlForm():
filePath=request.form['filePath']
#other stuff to do things with the data
return filePath
但是,当我提交任一表单时,它总是通过 getLoginForm() 函数。
我的表单如下,按照其各自功能的顺序排列。
<form action="." method="POST">
<input type="text" name="filePath">
<input type="submit" name="dropboxSubmit" value="Submit">
</form>
和
<form action="." method="POST">
<input type="text" name="filePath">
<input type="submit" name="dropboxSubmit" value="Submit">
</form>
有人介意帮我解决这个问题吗?谢谢!
最佳答案
我想问题出在使用“.”作为表单 Action 。相反,请使用要发布到的页面的实际路径。
<form action="/control" method="POST">
<input type="text" name="filePath">
<input type="submit" name="dropboxSubmit" value="Submit">
</form>
关于python - 从表单获取数据,但根据 Flask 所在的页面运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17099586/