简体   繁体   English

我究竟做错了什么? 似乎无法将参数传递给服务器

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

I am trying to have user enter a number which will call a query and show the results in a table for user to compare, but when the user submits the form, the python program gets the input and get the results properly. 我试图让用户输入一个数字,它将调用一个查询并在一个表中显示结果供用户比较,但是当用户提交表单时,python程序获取输入并正确获取结果。

In short, user enters a number and it generates a small table of results. 简而言之,用户输入一个数字,它会生成一个小的结果表。

Some reason the input is not been passed. 输入未被传递的一些原因。

Please check my work to see what is wrong. 请检查我的工作,看看有什么问题。

Here is main.py: 这是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);

Here is the index.html: 这是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>

Move the code from the template to the index() function and pass the lists as dictionary values. 将代码从模板移动到index()函数,并将列表作为字典值传递。

In general, try to restrict the programming logic in a template to a minimum required to render data. 通常,尝试将模板中的编程逻辑限制为呈现数据所需的最小值。

I don't understand what this line is supposed to be doing: 我不明白这条线应该做什么:

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

This is creating a dictionary with a single key, 'choice', whose value is either True if the request method is GET, or False otherwise. 这是使用单个键“choice”创建一个字典,如果请求方法是GET,则其值为True ,否则为False I highly doubt that's what you meant. 我非常怀疑这就是你的意思。 Perhaps you meant this: 也许你的意思是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM