Question

The project is a simple web crawler and and search engine. The "Index" handler has a form for entering the domain to search and the term to find. I would like the POST method to redirect to the "LuckySearch" handler, which searches for the regex term.

I've tried using web.redirect() and web.seeother(), but it seems that these functions do not support string substitution. How else can I solve this problem?

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()
Was it helpful?

Solution

The following line

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

should be changed to

raise web.seeother('/%s' % word)
  1. You have to use % on string, not web.redirect result.
  2. I think web.seeother should be used instead of web.redirect, because the latter returns 301 Moved Permanently redirect and I don't think you need permanent redirect here.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top