我在一个网页上有两个表单,其内容在分别提交时存储在两个不同的数据库中。
这是包含两种不同形式的源代码:
<form method = "post">
To:<input type = "text" name = "to"><br>
Amt:<input type = "text" name = "to_amt"> <br>
<input type = "submit">
</form>
<br>
<form method = "post">
By:<input type = "text" name = "by"><br>
Amt:<input type = "text" name = "by_amt"> <br>
<input type = "submit">
</form>
现在,当我从网页提交表单时,相关处理程序如何知道调用了哪个表单的 post 方法?当我在 main.py 文件中提交一个表单的内容时,如何区分这两个表单?
PS:我不想根据我提交的表单将应用程序的控件重定向到两个不同的 URL。还有其他替代解决方案吗?
最佳答案
添加一个隐藏字段来标识提交的表单,然后对代码进行适当的分支。
带注释的表单:
<form method = "post">
To:<input type = "text" name = "to"><br>
Amt:<input type = "text" name = "to_amt"> <br>
<input type = "hidden" name = "form_name" value = "first">
<input type = "submit">
</form>
然后只需检查您的处理程序:
def post(self):
if self.request.get('form_name') == 'first':
# the first form was submitted
关于python - 谷歌应用程序引擎(Python): Using multiple forms on a single web page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929654/