简体   繁体   English

使用url参数指定python如何处理请求

[英]use url parameter to specify how python handles the request

okay so this seemed like it should be pretty basic but I just can't get it to work. 好的,这似乎应该很基本,但我无法使其正常工作。 I am getting a 404 error saying that the resource cannot be located, though I am being directed to the correct address, www.url.com/sea?s='1' for sailing for example. 我收到404错误消息,提示无法找到资源,尽管我被定向到正确的地址,例如用于航行的www.url.com/sea?s='1'。 I have a list of links with different query parameters and I want them to be handled differently by my python code. 我有一个具有不同查询参数的链接列表,我希望它们的python代码以不同的方式处理它们。 I am using google app engine with python and a jinja2 template system. 我正在使用带有python和jinja2模板系统的google app引擎。

Here is my HTML: 这是我的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>

and here is the python: 这是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)

UPDATE: The problem wasn't actually with the code it was with my URL handling. 更新:问题实际上与我的URL处理代码无关。 This is correct: 这是对的:

app = webapp2.WSGIApplication([('/', MainPage),
                               (r'/sea', Sea)]

The 404 error is coming on not because of anything wrong in your page, but rather because something is wrong with your routes or your app.yaml file. 出现404错误的原因不是您的页面有任何问题,而是因为您的路线或app.yaml文件有问题。 If you are using webapp2, you just need to define a route that has the url r'/air' and it should work. 如果您使用的是webapp2,则只需定义一个路径,该路径的网址为r'/air'并且应该可以使用。 (eg webapp2.Route(r'/sea/', handler=Sea) (例如webapp2.Route(r'/sea/', handler=Sea)

By the way, instead of using query strings in your get request, you could put those in as route kwargs and do nicer things, eg (the syntax is <KEYWORDNAME:REGULAREXPRESSION> when no keyword name is given (like in <:/?> ) it just matches the regex and doesn't pass anything through to you) 顺便说一句,您可以在路由请求中使用查询字符串,而不必在get请求中使用查询字符串,并且可以做一些更好的事情,例如(当未指定关键字名称时,语法为<KEYWORDNAME:REGULAREXPRESSION> (例如<:/?> )它只与正则表达式匹配,不会传递任何东西给您

webapp2.Route(r'/sea<:/?><activity:[a-zA-Z]*?>', defaults={"activity":""}, handler=Sea, name="sea")

And then you could change your urls to, for example: 然后,您可以将网址更改为:

<a href="/sea/sailing">Sailing</a>

The only other change you need to make is in your Handler function. 您需要进行的唯一其他更改是在Handler函数中。 It needs to accept kwargs. 它需要接受变形。 (so you can literally just change your get request slightly): (因此您可以从字面上稍微更改get请求):

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

Which would simplify your code quite a bit and let you make it more flexible. 这将大大简化您的代码并使您更灵活。 Further, if your site doesn't update very often, you could do a bit of caching to make the queries snappier, etc. 此外,如果您的站点不经常更新,则可以进行一些缓存以使查询更加快速等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM