2017-07-27 67 views
-1

我想创建一个网页,需要用户输入并使用该输入作为一个独立的.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形式,我只是它是否可以做这样。 任何帮助表示赞赏

+0

'投票下来'应该附带一个解释。这将有所帮助。 – ana

回答

0

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

<form action="/" method="get">调用@app.route('/')而不是@app.route('/results')

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

+0

它没有改变我的结果。在app.py中调用my_func()可以吗? – ana

+0

@ana它取决于project.py文件的位置。你能提供项目树吗? –

+0

这是文件的位置:C:/ - > myproject - > env - > project.py – ana

0

您需要将您的表单直接转到带逻辑的页面/函数。将表单的action="/"更改为action="/results"

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

**来自审查队列:**我可以请求您请您在答案中添加更多的上下文。仅有代码的答案很难理解。如果您可以在帖子中添加更多信息,它可以帮助提问者和未来的读者。另请参阅[完全解释基于代码的答案](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)。 –