本文介绍了更改表格中标签的显示顺序(Google App Engine-Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在 UserAdminPage 中有一个表格,其中标签按字母顺序列出(下面的代码)。I have a table in UserAdminPage where tags are listed by alpha order (code below). I想要添加一个链接,以便按日期更改订单。我知道我会将 query.order(owner_tag)更改为 query.order( - date),但是我需要创建一个新的处理程序吗?I want to add a link that will change the order to by date. I know I will change the query.order(owner_tag") to query.order("-date") but do I need to create a new handler? 我不明白问号所在的链接应该是什么。I don't understand what the link should be where the question marks are.谢谢 Thanksclass UserAdminPage(webapp.RequestHandler): def get(self):...#-----------tags table-----------------# query = Owner.all() query.filter("owner =", user) query.order("owner_tag") w = query.fetch(500) user_tag_list = [] for item in w: user_tag_list.append(item.owner_tag) unique_tags = sorted(f1.f2(user_tag_list)) #-----------holding table start--------# self.response.out.write(""" <table border="0" cellpadding="0" cellspacing="20" > <tr> <td>""")#-----------tags table start--------# self.response.out.write("""<table border="1"> <tr> <th colspan="3">tags<br /> #the link that will sort by most recent: #<a href="?????">order tags by most recent</a></th> </tr> """) for tag in unique_tags: self.response.out.write(""" <tr> <td><a href="/tag?tag=%s">%s</a></td> </tr> """ % (tag, tag) ) self.response.out.write("""</table>""") #holding table first column end self.response.out.write("""</td>""") #holding table second column start self.response.out.write("""<td style="vertical-align:top">""") UPDATED CODE AS PER KEVIN P'S ANSWER:class UserAdminPage(webapp.RequestHandler): def get(self): order_by = self.request.get("order")...#-----------tags table-----------------# query = Owner.all() query.filter("owner =", user) if not order_by: query.order("owner_tag") elif order_by == "date": query.order("-date") w = query.fetch(500) user_tag_list = [] for item in w: user_tag_list.append(item.owner_tag) unique_tags = f1.f2(user_tag_list) #-----------holding table start--------# self.response.out.write(""" <table border="0" cellpadding="0" cellspacing="20" > <tr> <td>""")#-----------tags table start--------# # I put the link here but when I click on it, no table is drawn# because the `for` block below draws the tag table# so I don't understand how I can draw the table with `order=date` self.response.out.write("""<table border="1"> <tr> <th colspan="3">tags<br /> <a href="/tag?order=date"><span id=small>most recent</span></a></th> </tr> """) for tag in unique_tags: self.response.out.write(""" <tr> <td><a href="/tag?tag=%s">%s</a></td> </tr> """ % (tag, tag) ) self.response.out.write("""</table>""") ... 推荐答案有很多方法可以优化您的方法,但回答你的具体问题 - 而不是重复你的处理程序,你可以添加一个GET参数的链接,然后检查,而构建您的查询:There are a number of ways to optimize your approach to this, but to answer your specific question - rather than duplicate your handler you could add a GET parameter to the link and then check for that while constructing your query:class UserAdminPage(webapp.RequestHandler): def get(self): order_by = self.request.get('order') query = Owner.all() query.filter("owner =", user) if not order_by: query.order("owner_tag") elif order_by == 'date': query.order("-date") ... <a href="your_page_url?order=date">order tags by most recent</a> 这篇关于更改表格中标签的显示顺序(Google App Engine-Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 02:26