2017-07-27 63 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)。 –