Search code examples
jquerypythonhtmlgoogle-app-enginewebapp2

My html to webapp2 search form doesn't work


my search term comes through to my handler as an empty string. Here's the components:

html form:

<form class="navbar-search" action="/search">
                <input type="text" class="search-query span3 ajax-typeahead" placeholder="Search" data-link="/typeahead" name="searchQuery"  autocomplete="off" />
                <button class="btn btn-primary" style="margin-top: 0px;" type="submit">
                    <i class="icon-search icon-white"></i>
                </button>
            </form>

jquery for typeahead:

<script type="text/javascript">
            $(document).ready(function() {
                $('.ajax-typeahead').typeahead({
                    source: function(query, process) {
                        return $.ajax({
                            url: $(this)[0].$element[0].dataset.link,
                            type: 'get',
                            data: {query: query},
                            dataType: 'json',
                            success: function(json) {
                                return typeof json.options == 'undefined' ? false : process(json.options);
                            }
                        });
                    }
                });
            });
        </script>

python webapp2 handler:

class SearchHandler(webapp2.RequestHandler):
    def get(self, searchQuery):
        self.response.out.write('Term: %s' % searchQuery)

app = webapp2.WSGIApplication([('/search(.*)', SearchHandler)],
                              debug=True)

and the result:

enter image description here

I can't seem to find the problem, thanks for any help


Solution

  • In your RequestHandler do that:

    class SearchHandler(webapp2.RequestHandler):
      def get(self):
        searchQuery = self.request.get('searchQuery')
        self.response.out.write('Term: %s' % searchQuery)
    

    You are confusing the query parameters with the URL mappings.

    Also in Python I would suggest you to use the lower_case_underscore convention for variables.