Python 如何从POST方法重定向到web.py中的另一个处理程序

Python 如何从POST方法重定向到web.py中的另一个处理程序,python,redirect,http-post,web.py,string-substitution,Python,Redirect,Http Post,Web.py,String Substitution,该项目是一个简单的网络爬虫和搜索引擎。“索引”处理程序有一个表单,用于输入要搜索的域和要查找的术语。我希望POST方法重定向到“LuckySearch”处理程序,该处理程序搜索regex术语 我尝试过使用web.redirect()和web.seeother(),但这些函数似乎不支持字符串替换。我还能怎样解决这个问题 class Index(object): def GET(self): form = searchform() return render.

该项目是一个简单的网络爬虫和搜索引擎。“索引”处理程序有一个表单,用于输入要搜索的域和要查找的术语。我希望POST方法重定向到“LuckySearch”处理程序,该处理程序搜索regex术语

我尝试过使用web.redirect()和web.seeother(),但这些函数似乎不支持字符串替换。我还能怎样解决这个问题

class Index(object):
    def GET(self):
        form = searchform()
        return render.formtest(form)

    def POST(self):
        form = searchform()
        if not form.validates():
            return render.formtest(form)
        else:
            word = form['word'].get_value()
            print "You are searching %s for the word %s" % (form['site'].get_value(), word)
            raise web.redirect('/%s') % word

class LuckySearch(object):
    def GET(self, query):
        query = str(query)
        lucky = lucky_search(corpus, query)
        ordered = str(pretty_ordered_search(corpus, query))
        if not lucky:
            return "I couldn't find that word anywhere! Try google.com instead."
        else:
            return "The best page is: " + lucky + "\n" + "but you might also try:" + "\n" + ordered

class About(object):
    def GET(self):
        return "This is my first search engine! It only runs on my local machine, though."

if __name__ == "__main__":
    corpus = crawl_web('http://en.wikipedia.org/wiki/Trinity_Sunday', 'http://en.wikipedia.org/wiki/Trinity_Sunday')
    app = web.application(('/', 'Index', '/about', 'About', '/(.*)', 'LuckySearch'), globals())
    app.run()
下一行

raise web.redirect('/%s') % word
应改为

raise web.seeother('/%s' % word)
  • 您必须在字符串上使用
    %
    ,而不是
    web。重定向
    结果
  • 我认为应该使用
    web.seeother
    而不是
    web.redirect
    ,因为后者返回
    301永久移动的
    redirect,我认为您不需要在这里永久重定向
  • 下一行

    raise web.redirect('/%s') % word
    
    应改为

    raise web.seeother('/%s' % word)
    
  • 您必须在字符串上使用
    %
    ,而不是
    web。重定向
    结果
  • 我认为应该使用
    web.seeother
    而不是
    web.redirect
    ,因为后者返回
    301永久移动的
    redirect,我认为您不需要在这里永久重定向

  • 那很有效!感谢您提供关于web.redirect vs.web.seether.的提示,效果非常好!感谢您提供关于web.redirect vs.web.seether的提示。