簡體   English   中英

我究竟做錯了什么? 似乎無法將參數傳遞給服務器

[英]what am i doing wrong? cant seem to pass a parameter to server

我試圖讓用戶輸入一個數字,它將調用一個查詢並在一個表中顯示結果供用戶比較,但是當用戶提交表單時,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>

將代碼從模板移動到index()函數,並將列表作為字典值傳遞。

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

我不明白這條線應該做什么:

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

這是使用單個鍵“choice”創建一個字典,如果請求方法是GET,則其值為True ,否則為False 我非常懷疑這就是你的意思。 也許你的意思是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM