【问题标题】:python bottle framework post redirect [duplicate]python瓶子框架后重定向[重复]
【发布时间】:2014-12-03 15:44:01
【问题描述】:

我正在编写一个简单的前端网页,它将接收一串单词并返回一个单词计数表。现在我可以查询字符串并将结果发布在同一页面上。但是,我想重定向并将结果发布到另一个页面上,应该是 @post(/'result')

下面是我的代码,但是,它一直给我一个错误提示:异常:

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'

我应该改变什么,以便在重定向页面 /result 上发布结果表而不会导致错误?

@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)

【问题讨论】:

    标签: python http post bottle


    【解决方案1】:

    我猜第一个请求的 POST 参数在重定向中丢失了。我不确定您想要实现什么,但您可以首先使用不同的 POST URL 来完全省略重定向:

    @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)
    

    另外,请注意有几个用于重定向的 HTTP 代码(请参阅https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection)。据我所知,Bottle 0.7+ 使用 HTTP 响应代码 303。您可以尝试在重定向中使用重定向代码 307 来使其正常工作(将其用作第二个参数)。

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      相关资源
      最近更新 更多