为什么这个光标不起作用?

时间:2011-09-22 23:49:25

标签: python google-app-engine cursor

你知道为什么这个光标不起作用吗?当我点击“更多结果”时,不会显示其他结果。感谢。

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)

修改

As Dave W. Smith's answer问题是s在调用光标后发生了变化。我在代码下面粘贴了日志信息。

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="/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")

修改2

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]))
...                           

1 个答案:

答案 0 :(得分:2)

假设代码在处理程序中,每个调用的s[0]是否相同?游标仅适用于相同声明的查询。如果s[0]发生更改,则查询会更改,之前保存的游标将无法使用。