这是我的HTML:
<h3><a href="/" class="center-it">Quick Navigation</a></h3>
<div class="span1">
<div class="span1">
<h4><a href="/sea">Sea</a></h4>
<ul>
<li><a href="/sea?s='1'">Sailing</a></li>
<li><a href="/sea?s='2'">Diving</a></li>
<li><a href="/sea?s='3'">Surfing</a></li>
<li><a href="/sea?s='4'">Kite Boarding</a></li>
<li><a href="/sea?s='5'">Kayaking</a></li>
</ul>
</div>
这是python:
class Sea(BlogHandler):
def get(self, s):
s = self.request.get('s')
if s == '1':
posts = posts = db.GqlQuery("select * from Post where sport=:1 order by created desc limit 30", "sailing")
elif s == '2':
posts = posts = db.GqlQuery("select * from Post where sport=:1 order by created desc limit 30", "diving")
elif s == '3':
posts = posts = db.GqlQuery("select * from Post where sport=:1 order by created desc limit 30", "surfing")
elif s == '4':
posts = posts = db.GqlQuery("select * from Post where sport=:1 order by created desc limit 30", "kiteboarding")
elif s == '5':
posts = posts = db.GqlQuery("select * from Post where sport=:1 order by created desc limit 30", "kayaking")
else:
posts = posts = db.GqlQuery("select * from Post where element=:1 order by created desc limit 30", "sea")
global visits
user = users.get_current_user()
logout = users.create_logout_url(self.request.uri)
self.render('sport.html', user = user, posts=posts, visits = visits, logout=logout)
更新: 实际上问题不在于我的URL处理代码。这是正确的:
app = webapp2.WSGIApplication([('/', MainPage),
(r'/sea', Sea)]
答案 0 :(得分:1)
404错误的出现不是因为您的网页有任何问题,而是因为您的路线或app.yaml文件出现了问题。如果您使用的是webapp2,则只需要定义一个包含网址r'/air'
的路由,它应该可以使用。 (例如webapp2.Route(r'/sea/', handler=Sea)
顺便说一下,你可以把它们放在你的get请求中,而不是在你的get请求中使用查询字符串,而是将它们作为路径kwargs并做更好的事情,例如(如果没有给出关键字名称,语法为<KEYWORDNAME:REGULAREXPRESSION>
(如在<:/?>
中),它只匹配正则表达式并且不会将任何内容传递给您)
webapp2.Route(r'/sea<:/?><activity:[a-zA-Z]*?>', defaults={"activity":""}, handler=Sea, name="sea")
然后你可以改变你的网址,例如:
<a href="/sea/sailing">Sailing</a>
您需要做的唯一其他更改是在您的处理程序功能中。它需要接受kwargs。 (所以你可以稍微改变你的获取请求):
get(self, *args, **kwargs):
activity = kwargs.get("activity")
if activity in ("sailing", "kayaking", "hiking", "kiteboarding", "surfing", "diving")
posts = db.GqlQuery("select * from Post where sport=:1 order by created desc limit 30", activity)
elif activity:
self.error(404)
else:
posts = db.GqlQuery ... etc
这将简化您的代码并让您更灵活。此外,如果您的网站不经常更新,您可以进行一些缓存以使查询更加快捷等。