开发者

How to use dynamic url in two handlers in Google App Engine?

How do I take this url

<a href="/item?id=%s"> 

from this handler

class Newest(webapp.RequestHandler):
    def get(self):
            items = db.GqlQuery("SELECT * FROM Item ORDER BY date DESC LIMIT 30")

        self.response.out.write("<ol>")
        for item in items:
            self.response.out.write("""<li><a href="%s">%s</a> <br /><a href="/item?id=%s">comments</a></li><br /> """ % 
                                    (cgi.escape(item.url), cgi.escape(item.title), cgi.escape(str(item.key().id()))))
        self.response.out.write("</ol>")

and use it in this handler

class Item(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""<p>Article title and link goes here</p>""")

Thanks for your help!

EDIT

As suggested by djidjadji at app engine group I changed the url like this:

class Newest(webapp.RequestHandler):
    def get(self):
        items = db.GqlQuery("SELECT * FROM Item ORDER BY date DESC LIMIT 30")
        self.response.out.write("<ol>")
        for item in items:
            self.response.out.write("""<li><a href="%s">%s</a> <br /><a href="/item/%s"><span style="color: #808080; font-size: x-small;">comments</span></a></li><br /> """ % 
                                    (cgi.escape(item.url), cgi.escape(item.title), cgi.escape(str(item.key().id()))))
        self.response.out.write("</ol>")
        self.response.out.write("</div><开发者_运维问答/body></html>")

and Article handler like this:

class Article(webapp.RequestHandler):
    def get(self, id):
        id = int(id)        
        self.response.out.write("""<a href="/item/%s">article title</a> """ % 
                                    (id))

So now the url is correct. But can you help with the article title? How do I get the article title item.title from inside the for loop to the Article handler?

EDIT 2

This solves the problem. (Thanks djidjadji)

class Article(webapp.RequestHandler):
    def get(self, id):
        id = int(id)
        item = Item.get_by_id(id)

        self.response.out.write("""<a href="%s">%s</a> """ % 
                                    (cgi.escape(item.url), cgi.escape(item.title)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜