Search code examples
pythonhttppostbottle

python bottle framework post redirect


Im writing a simple front end webpage that would take a string of words and return a table of words count. For now i'm able to query the string and post the result on the same page. However, I want redirect and post the result on another page with should be @post(/'result')

Below is my code, however, it keep giving me an error saying: Exception:

AttributeError  ("'NoneType' object has no attribute 'split'",)
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/Library/Python/2.7/site-packages/bottle.py", line 1729, in wrapper
    rv = callback(*a, **ka)
  File "frontEnd.py", line 16, in result
  File "frontEnd.py", line 23, in query
    for word in keyString.split():
AttributeError: 'NoneType' object has no attribute 'split'

what should I change so that I post the result table on the redirected page /result without causing an error?

@get('/')
def search():
    return '''<h1>Search</h1><form action="/" method="post"> Keyword:<input name="keywords"type="text"/><input value="GO" type="submit" /> </form>'''

@post('/')
def do_search():
    redirect('/result')

@post('/result')
def result(wc,keyString):
    keyString = request.forms.get('keywords')
    wc = query(keyString)
    return wordCountHTML(wc,keyString)

Solution

  • I guess the POST parameters of the first request are getting lost in the redirect. I am not sure what you want to achieve, but you could entirely omit the redirect by using a different POST URL in the first place:

    @get('/')
    def search():
        return '''<h1>Search</h1><form action="/result" method="post"> Keyword:<input name="keywords"type="text"/><input value="GO" type="submit" /> </form>'''
    
    @post('/result')
    def result(wc,keyString):
        keyString = request.forms.get('keywords')
        wc = query(keyString)
        return wordCountHTML(wc,keyString)
    

    Also, please note that there are several HTTP codes for redirection (see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection). As far as I know, Bottle 0.7+ uses HTTP response code 303. You could try to use a redirect code 307 in your redirect instead to make it work (use it as a second parameter).