2016-04-24 57 views
0

我正在嘗試編寫代碼,它將爲頁面請求數據,處理它,然後在不刷新的情況下在頁面的較低部分顯示結果。在Flask的同一頁面上顯示結果

瓶代碼:

if request.method == 'GET': 
     return render_template("dashboard.html", data_nodes=data_nodes, data_para=data_para) 
    elif request.method=='POST': 
     #c, conn = connection2() 
     select1 = request.form.get('node') 
     select2 = request.form.get('parameter') 
     print str(select1) 
     print str(select2) 
     #processes the data 
     #display the processed data on the same page 
     #return render_template("dashboard.html") This will refresh the page! Is there another way? 

我的HTML側

 <form method="POST" action="{{ url_for('dashboard')}}"> 
      <label for="node">Node</label> 
      <select name="node"> 
       {% for o in data_nodes %} 
       <option value="{{ o.name }}">{{ o.name }}</option> 
       {% endfor %} 
      </select> 
      <label for="parameter">Parameter</label> 
      <select name="parameter"> 
       {% for o in data_para %} 
       <option value="{{ o.name }}">{{ o.name }}</option> 
       {% endfor %} 
      </select> 
      <button type="submit" class="btn btn-default" >Send</button> 
     </form> 

有沒有一種方法,而無需重定向到另一個頁面後處理的數據?

+0

請記住,當您想要在頁面中顯示某些內容而無需刷新時,您需要在頁面上使用Javascript進行所有更新。所以要做你想做的事情的方法是製作一個JavaScript來調用你的服務,然後用收到的數據來更新你的部分。 – Aquiles

+0

這是否意味着JavaScript將首先獲取數據,並將其傳遞給Flask,然後Flask將處理數據並將其發送回JavaScript?那可能嗎? –

+1

是的,這樣做的方法是讓Javascript,AJAX發送表單到你的瓶子後期服務,然後處理你的服務的響應。查看答案中的示例@sashadereh – Aquiles

回答

1

我認爲AJAX是你確切需要的。 Here你可以找到工作示例,只需運行它即可查看結果。

+0

謝謝先生。這正是我想要的,但顯然由於某些原因它不適合我。我更好地爲此打開另一個問題。再次感謝你。 –

相關問題