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>
4

2 回答 2

1

我不明白这条线应该做什么:

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

这是使用单个键“选择”创建一个字典,其值为True请求方法是否为 GET 或False其他。我非常怀疑这就是你的意思。也许你的意思是:

return {'choice': request.GET.get('choice')}
于 2012-03-04T22:50:49.527 回答
1

将代码从模板移动到index()函数,并将列表作为字典值传递。

通常,尝试将模板中的编程逻辑限制为呈现数据所需的最低限度。

于 2012-03-04T21:11:06.927 回答