Flask中的表格不起作用

时间:2017-07-27 17:45:17

标签: python html forms flask

我正在尝试创建一个获取用户输入的网页,并将该输入用作单独的.py文件中的参数(因此.py文件将字符串作为参数)

.py文件可以正常工作。但我在html中的表单似乎不正确。我有类似的东西,但没有找到我正在寻找的东西。

这是我的html:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form action="/" method="get">
  <center>
    <div>
      Keyword: <input type="text" name="search" style="display:inline; width:482px;margin-top:50px" title="Enter a keyword">
      <input type="submit" value="Analyze" title="Click to search">
    </div>
     </center>
</form>

在这种情况下可以使用GET。

这是我的app.py

from flask import Flask 
from flask import request 
from flask import render_template
from project import my_func
import json, urllib 

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template("index.html") #Set render template

@app.route('/results', methods=['GET', 'POST'])
def index():

    if request.method == 'GET':
        search= request.args.get('search')
        my_func(search) #passes the user input into the my_func as a parameter
        return  render_template('index.html')
    else:
        return render_template('index.html')
if __name__ == '__main__':
    app.debug = True #Uncomment to enable debugging
    app.run() #Run the Server

我认为错误是GET形式,我只是可以这样做。 任何帮助表示赞赏

2 个答案:

答案 0 :(得分:0)

我相信表单的操作会将您发送回my_form函数。

<form action="/" method="get">来电@app.route('/')而非@app.route('/results')

尝试将表单操作更改为/results

答案 1 :(得分:0)

您需要使用逻辑将表单指向页面/功能。将您的表单action="/"更改为action="/results"

<form action="/results" method="get">