Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...