2013-04-22 50 views
1

該項目是一個簡單的網絡爬蟲和搜索引擎。 「索引」處理程序具有用於輸入要搜索的域和要查找的術語的表單。我希望POST方法重定向到「LuckySearch」處理程序,它可以搜索正則表達式項。如何從POST方法重定向到web.py中的另一個處理程序

我試過使用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() 

回答

1

下面一行

raise web.redirect('/%s') % word 

應改爲

raise web.seeother('/%s' % word) 
  1. 你必須使用%的字符串,而不是web.redirect結果。
  2. 我認爲web.seeother應該用來代替web.redirect,因爲後者返回301 Moved Permanently重定向,我不認爲你需要永久重定向到這裏。
+0

完美的工作!感謝您對web.redirect與web.seeother的提示。 – Dan 2013-04-25 04:42:42

相關問題