2011-09-22 50 views
0

你知道這個遊標爲什麼不起作用嗎?當我點擊「更多結果」時,不會顯示其他結果。謝謝。這個光標爲什麼不能正常工作?

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史密斯:

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

查詢在哪裏?你怎麼知道會有更多的結果呢? –

+0

我向該問題添加了查詢。我使用'items = query.fetch(10)'進行測試,並且有4個項目與此查詢匹配。謝謝。 – Zeynel

回答

2

假設代碼是在處理程序中,是s[0]在每次調用相同的?遊標只能用於相同的聲明查詢。如果s[0]發生更改,則查詢更改,並且以前保存的光標將無法使用。

+0

非常感謝!這正是問題所在。我從搜索表單中取出's',但在光標後面(當我點擊「更多結果」時),搜索表單是空的,並且s = []',所以我認爲這是一個不同的查詢,並且沒有返回結果。你知道這個方法嗎?我用代碼編輯了包括日誌信息的問題。再次感謝。 – Zeynel

+0

在你的位置,我會在URL中包含前面的s [0]值和光標。然後,如果值已更改,我會放棄保存的光標並從頭開始查詢。 –

+0

謝謝!我在解決方案上面添加了。也許最好問一個新問題,但我也想添加分頁,以便人們可以回到以前的結果。我只能找到Java的相關代碼:http://groups.google.com/group/google-appengine-java/browse_thread/thread/da8b96441b0ae038您可以提供有關如何在Python中進行分頁的任何線索嗎?再次感謝。 – Zeynel

相關問題