Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

python - How to use cursor() for pagination?

Can anyone point me to a practical application of cursor() to do pagination?

I am not clear how to use cursor() as given in the documentation.

This is my query:

items = db.GqlQuery("SELECT * FROM Item ORDER BY date DESC LIMIT 30")

which I render like this:

self.response.out.write("<ol>")
for item in items:
    self.response.out.write("""<li><a href="/vote/%s?type=%s"> ^ </a><a href="%s">%s</a> <span id='Small'>(%s)</span><br /> 
        <div id='Small'> 
        %s points %s by %s <a href="/item/%s"></a> | 
        <a href="/item/%s#disqus_thread"></a></div>
        </li><br /> """ % 
        (str(item.key().id()), merchandise_type, item.url, item.title, urlparse(item.url).netloc,
        item.points, item.date.strftime("%B %d, %Y %I:%M%p"), item.user_who_liked_this_item, str(item.key().id()), str(item.key().id())))                               

   self.response.out.write("</ol>")

Thanks!

UPDATE

Hi Amir: Thanks for your answer but I could't make this link work. Here's what I have:

#===========adding cursor here===========#
            cursor = self.request.get("cursor")
            if cursor: query.with_cursor(cursor)
            items = query.fetch(30)
            cursor  = query.cursor()

#===========adding cursor here===========#

#===========regular output===========#
            self.response.out.write("<ol>")
            for item in items:
                self.response.out.write("""<li>
                <a href="/vote/%s?type=%s"> ^ </a><a href="%s">
                <span id="large">%s</span></a> 
                <span id='Small'>(%s)</span>
                <br />  
                %s<br /> <span id='Small'> 
                %s points %s by %s <a href="/item/%s"></a> | 
                <a href="/item/%s#disqus_thread"></a>
                </span>
                </li><br /> """ %           
                (str(item.key().id()), merchandise_type, item.url, 
                item.title, urlparse(item.url).netloc, 
                item.summary, item.points, item.date.strftime("%B %d, %Y %I:%M%p"), 
                item.user_who_liked_this_item, str(item.key().id()),  
                str(item.key().id())))                               

            self.response.out.write("</ol>")
#===========regular output===========#

#===========link to cursor===========#
            self.response.out.write("""<a href="/dir?type=%s?cursor=%s">Next
            Page</a>""" % (merchandise_type, cursor))

But this results in this url which displays nothing:

http://localhost:8083/dir?type=newest?cursor=E9oBdgoTc2FyYWgtZm9yLXByZXNpZGVudBoESXRlbUtSBGRhdGVYAkwhQ1VSU09SIWoiahNzYXJhaC1mb3ItcHJlc2lkZW50cgsLEgRJdGVtGKsCDHIVCAcaBGRhdGUgACoJCMid8OXW4qYCggELCxIESXRlbRirAgzgAQAU

Update 2

See @Amir comment below: changing the second ? to & solved the problem. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...