Question

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.

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:

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:

<!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>
Was it helpful?

Solution

Move the code from the template to the index() function and pass the lists as dictionary values.

In general, try to restrict the programming logic in a template to a minimum required to render data.

OTHER TIPS

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. I highly doubt that's what you meant. Perhaps you meant this:

return {'choice': request.GET.get('choice')}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top