2012-03-04 26 views
0

我想讓用戶輸入一個數字,這將調用一個查詢,並顯示在表中的結果供用戶比較,但是當用戶提交表單時,python程序獲取輸入並正確地得到結果。我做錯了什麼?似乎不能傳遞一個參數到服務器

總之,用戶輸入一個數字,它會生成一個小的結果表。

輸入未被通過的一些原因。

請檢查我的工作,看看有什麼不對。

這裏是main.py:

from bottle import request, route, run, view 

@route('/') 
@view('index.html') 
def index(): 
    print request.GET.get('choice'); 
    return dict(choice = request.method == 'GET'); 

run(host = 'localhost', port = 9988); 

這裏是index.html的:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Search Engine Comparator!</title> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
     <style> 
      #navlist li 
      { 
       display: inline; 
       list-style-type: none; 
       padding-right: 20px; 
      } 
     </style> 
    </head> 
    <body> 
     % from bSearch import * 
     % from gSearch import *  
     % from termList import *   
     <center><h2>Search Engine Comparator</h2></center> 
     <div id="navcontainer" align="center"> 
      <ul id="navlist"> 
       <ul> 
        % r1 = getList1(); 
        % for r in r1: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r2 = getList2(); 
        % for r in r2: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r3 = getList3(); 
        % for r in r3: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r4 = getList4(); 
        % for r in r4: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r5 = getList5(); 
        % for r in r5: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
      </ul> 
      <ul id="navlist"> 
       <ul> 
        % r6 = getList6(); 
        % for r in r6: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r7 = getList7(); 
        % for r in r7: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r8 = getList8(); 
        % for r in r8: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r9 = getList9(); 
        % for r in r9: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
       <ul> 
        % r10 = getList10(); 
        % for r in r10: 
        <li> {{ r }} </li> 
        % end 
       </ul> 
      </ul> 
     </div> 
     % choice = request.GET.get('choice'); 
     % if choice: 
     <form action="/" method="get" id="resForm" name="resForm"> 
      <div align="center"> 
       <label for="choice">Enter which list to query:</label> 
       <input type="text" name="choice" /> 
       <input type="submit" value="Submit" /> 
      </div>   
     % else: 
     <form action="" method="get" id="resForm" name="resForm"> 
      <div align="center"> 
       <label for="choice">Enter which list to query:</label> 
       <input type="text" name="choice" /> 
       <input type="submit" value="Submit" /> 
      </div>    
      % queries = getQueries(choice); 
      % for q in queries: 
      <table border="1" width="100%"> 
       <tr> 
        <th></th><th>The query is: {{ q }}</th><th></th> 
       </tr> 
       <tr> 
        <td align="center"><input type="checkbox" id="b" name="bing">I pick this!!!</td> 
        <td align="center"><input type="checkbox" id="g" name="goog">I pick this!!!</td> 
        <td align="center"><input type="checkbox" id="y" name="yhoo">I pick this!!!</td>     
       </tr>    
       <tr>      
        <td width="33%"> 
         <ol> 
          % bRes = bSearch(q);   
          % for b in bRes[:8]: 
          <li>{{ b.title }} <br /> <a href={{ b.url }}>{{ b.url }}</a></li> 
          % end 
         </ol> 
        </td> 
        <td width="33%"> 
         <ol> 

         </ol> 
        </td> 
        <td width="33%"> 
         <ol> 
          <li>aint working b!</li> 
         </ol> 
        </td>     
       </tr> 
       <br /> 
       % end  
      </table>    
      % end 
      <center><br /><input type="button" id="checker" value="Click to get the count!" onclick="checkerCount()" /></center> 
     </form>  
    </body> 
</html>  

<script type="text/javascript"> 
function checkerCount() 
{ 
    var inputTags = document.getElementsByTagName('input'); 
    var size = inputTags.length; 
    var b = 0; 
    var g = 0; 
    var y = 0; 
    for (var i = 0; i < size; i++) 
    { 
     if(inputTags[i].type=='checkbox' && inputTags[i].checked && inputTags[i].id=='b') { b++; } 
     if(inputTags[i].type=='checkbox' && inputTags[i].checked && inputTags[i].id=='g') { g++; } 
     if(inputTags[i].type=='checkbox' && inputTags[i].checked && inputTags[i].id=='y') { y++; } 
    } 
    alert("After counting and all that, we found the scores!\n" + 
      "Bing has score: " + b + 
      "\nGoogle has score: " + g + 
      "\nYahoo has score: " + y); 
} 
</script> 

回答

1

從模板到index()功能移動代碼並通過列表作爲字典的值。

通常,嘗試將模板中的編程邏輯限制爲呈現數據所需的最小值。

+0

能否請您解釋一下,在愚蠢的人而言..所以基本上將所有「%Python代碼」索引()? – iCodeLikeImDrunk 2012-03-04 23:17:07

+0

@Joe Chen:是的。你明白這個權利。這不是你的直接問題,但如果你移動代碼它也應該解決它自己。模板語言允許任意Python代碼的事實並不意味着把它放在那裏是一個好主意。順便說一句,你不需要把所有的代碼放在單個'index()'函數中。 – jfs 2012-03-05 00:08:30

1

我不明白這條線應該做的事情:

return dict(choice = request.method == 'GET'); 

這是創建與單個鍵,「選擇」,它的值是True如果任一方請求方法是GET字典,否則爲False。我非常懷疑這就是你的意思。也許你的意思是這樣的:

return {'choice': request.GET.get('choice')} 
+0

我從網上得到了那個地方,我正在考慮返回的選擇,所以HTML頁面將知道哪個函數調用.. – iCodeLikeImDrunk 2012-03-04 23:27:31

+0

好,但你的代碼沒有返回的選擇。它返回請求方法是否爲GET的布爾值。 – 2012-03-05 00:03:40

+0

@JoeChen:您可能是指[我的回答](http://stackoverflow.com/a/9541298/4279)中的代碼,其中'index()'同時提供GET和POST請求。我已經做了這個例子來簡化示例(我應該明確指出它不是*將任意Python代碼放入模板的邀請)。在你的情況下,index()只提供GET請求,所以request.method總是GET。從index()返回'dict(key = 123)'意味着你可以使用'{{key}}'在模板中插入'123'('index.html')。 – jfs 2012-03-05 00:39:00

相關問題