Why is this cursor not working?
Do you know why this cursor is not working? When I click on "More Results" no other results are displayed. Thanks.
if n==1:
query = Main.all()
query.filter("tag_list", s[0])
query.order("-date")
cursor = self.request.get("cursor")
if cursor: query.with_cursor(cursor)
items = query.fetch(2)
cursor = query.cursor()
for item in items:
main_id = item.key().id()
self.response.out.write("""<p>
<a href="%s" target="_blank"><span id=large>%s</span></a>
<a href="/comment?main_id=%s"><span id="small">comments</span></a><br />
%s <a href="/edit?main_id=%s&url_path=/searchhandler?search_string=%s"><span id="small">edit</span></a>
</p>
""" %
(item.url, item.title, main_id,
f1.truncate_at_space(item.pitch), main_id, search_string))
self.response.out.write('<a href="/searchhandler?cursor=%s">More Results</a>' % cursor)
Edit
As Dave W. Smith's answer the problem was with s
which changed after cursor is called. I paste below the code with log info.
class SearchHandler(webapp.RequestHandler):
def get(self):
...
#-------search form--------#
self.response.out.write("""
<form name="search_form" action="/searchhandler" method="get"><br />
<input type="text" name="search_string" size=40>
<input type="submit" value="search tags">
</form> """)
search_string = self.request.get("search_string")
s = filter(None, f1.striplist(self.request.get("search_string").split(" ")))
logging.info("""((((((s1: %s))))))""" % s)
# before cursor --> ((((((s1: [u'python']))))))
# after cursor --> ((((((s1: []))))))
n = len(s)
if n==1:
query = Main.all()
query.filter("tag_list", s[0])
query.order("-date")
logging.info("""((((((s2: %s))))))""" % s)
#-->((((((s2: [u'python']))))))
cursor = self.request.get("cursor")
if cursor: query.with_cursor(cursor)
items = query.fetch(2)
cursor = query.cursor()
related_tags = []
if items:
logging.info("""((((((s3: %s))))))""" % s)
#-->((((((s3: [u'python']))))))
for item in items:
for tag in item.tag_list:
related_tags.append(tag)
unique_tags = sorted(f1.f2(related_tags))
for tag in unique_tags:
self.response.out.write("""
<a href开发者_如何学C="/rt?rt=%s">%s</a> | """ %
(tag, tag))
self.response.out.write("""<br />""")
for item in items:
main_id = item.key().id()
self.response.out.write("""<p>
<a href="%s" target="_blank"><span id=large>%s</span></a>
<a href="/comment?main_id=%s"><span id="small">comments</span></a><br />
%s <a href="/edit?main_id=%s&url_path=/searchhandler?search_string=%s"><span id="small">edit</span></a>
</p>
""" %
(item.url, item.title, main_id,
f1.truncate_at_space(item.pitch), main_id, search_string))
self.response.out.write("""<a href="/searchhandler?cursor=%s">More Results</a>""" % cursor)
logging.info("""((((((s4: %s))))))""" % s)
# --> ((((((s4: [u'python']))))))
self.response.out.write("""<br /><br />""")
else:
self.redirect("/nomatch")
Edit 2
Problem solved as suggested by Dave Smith:
class SearchHandler(webapp.RequestHandler):
def get(self):
...
search_string = self.request.get("search_string")
if search_string:
s = filter(None, f1.striplist(self.request.get("search_string").split(" ")))
self.response.out.write("""
<form name="search_form" action="/searchhandler" method="get"><br />
<input type="text" name="search_string" size=40 value="%s">
<input type="submit" value="search tags">
</form> """ % search_string)
else:
ss = self.request.get("ss")
s = filter(None, f1.striplist(self.request.get("ss").split(" ")))
self.response.out.write("""
<form name="search_form" action="/searchhandler" method="get"><br />
<input type="text" name="search_string" size=40 value="%s">
<input type="submit" value="search tags">
</form> """ % ss)
n = len(s)
if n==1:
query = Main.all()
query.filter("tag_list", s[0])
query.order("-date")
cursor = self.request.get("cursor")
if cursor: query.with_cursor(cursor)
items = query.fetch(7)
cursor = query.cursor()
...
self.response.out.write("""<a href="/searchhandler?cursor=%s&ss=%s">More Results</a>""" % tuple([cursor, search_string]))
...
Assuming that code is in a handler, is s[0]
the same on each invocation? Cursors only work on identically declared queries. If s[0]
changes, the query changes, and the previously-saved cursor won't work with it.
精彩评论