形式在瓶不工作
问题描述:
我想创建一个网页,需要用户输入并使用该输入作为一个独立的.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形式,我只是它是否可以做这样。 任何帮助表示赞赏
答
您需要将您的表单直接转到带逻辑的页面/函数。将表单的action="/"
更改为action="/results"
。
<form action="/results" method="get">
+0
**来自审查队列:**我可以请求您请您在答案中添加更多的上下文。仅有代码的答案很难理解。如果您可以在帖子中添加更多信息,它可以帮助提问者和未来的读者。另请参阅[完全解释基于代码的答案](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)。 –
'投票下来'应该附带一个解释。这将有所帮助。 – ana