tknv
tknv

Reputation: 562

How to coding match '/search?query=a'

I want to search by query, like a below.

http://localhost:8080/search?query=a

html

...
    <form class="navbar-form navbar-right" name="searchform" action="/search" method="post">
    <div class="form-group">
        <input class="form-control" placeholder="Any keyword" type="text" name="query" />
    </div>
        <button class="btn btn-success" type="submit">Search</button>
    </form>  
...

routes.py

...
import handlers

_routes = [
    RedirectRoute('/search?query=<.*>', handlers.SearchHandler, name='search', handler_method='post')
]
...

handlers.py

...
class SearchHandler(BaseHandler):
    def get(self):
        params = {
            'query': '',
            'offset': '0'
        }
        self.doSearch(params)

    def doSearch(self, params):
        docs = search.Index(name='indexed_doc')
        query = params.get('query', '')
        try:
            offset_value = int(params.get('offset' or 0))
        except ValueError:
            offset_value = 0
        try:
            search_query = search.Query(...

result against: '/search?query=<.*>' at routes.py
error_handler.py:71] Error 403: Access was denied to this resource.
module.py:593] default: "POST /search HTTP/1.1" 403 2555

result against: '/search(.*)' at routes.py
error_handler.py:71] Error 404: The resource could not be found.
module.py:593] default: "POST /search HTTP/1.1" 404 2543

How can I coding regex for it? or what is wrong?
Thanks in advance.

Upvotes: 1

Views: 139

Answers (1)

dm03514
dm03514

Reputation: 55962

I believe there are a couple issues:

  1. Do you really want a RedirectRoute? is it necessary to redirect on post request?
  2. When you are posting data the data (query) should be stored in the request body, not in the url, like /search?query=a+query
  3. In many projects that I have seen, urls regexes do not include GET parameters, it is usually up to handlers to enforce certain get params, I don't know if GET param order is ensured if there are multiple params...

you could change to get request:

     <form class="navbar-form navbar-right" name="searchform" action="/search" method="get">

    <div class="form-group">
        <input class="form-control" placeholder="Any keyword" type="text" name="query" />
    </div>
        <button class="btn btn-success" type="submit">Search</button>
    </form>  

import handlers

_routes = [
    ('/search', handlers.SearchHandler, name='search')
]

Upvotes: 3

Related Questions