Friendly URLs in Google App Engine

I am trying to create a Friendly URL search method in GAE and Python. Has anyone done this?

It works for me, but it is very VERY shaky. How it works is that it takes a path (let them say / foo / bar /) and splits it into a list. Accepts the last item in the list and queries the database for matching. Now comes some kind of headache.

If there is another “bar”, as there may be another “bar”, but on a different page, how to tell them apart? At the moment, my solution is to iterate over the result of "bar" and look at the property of the link to the parent page. The parent "foo" could also happen more than once, so now we have to iterate over this value. It is easy to see that this cloud is easy to fold for a large number of cycles.

Just to make it a little worse, one page could work in more than one language, and the URL should match the language of Denmark.

My current data warehouse setup:

## Class to store pages.
class Pages(db.Model):
    name = db.StringProperty()
    ## Some more properties for storing content
    parentKey = db.SelfReferenceProperty()

## Class to store friendly url path segments
class UrlProvider(db.Model):
    name = db.StringProperty()
    path = db.StringProperty()
    langCode = db.StringProperty()
    page = db.ReferenceProperty(Pages)

And to get the page using firendly url:

pageFromUrl = UrlProvider.gql('WHERE path = :path AND langCode = :lang', path = path, lang = templateObject.lang).fetch(100)

for pageUrl in pageFromUrl:
    parentFromUrl = UrlProvider.gql('WHERE page = :page AND langCode = :lang', page = pageUrl.page.parentKey, lang = templateObject.lang).fetch(100)
    for parentUrl in parentFromUrl:
        if parentUrl.path == templateObject.path[-2]:
            templateObject.url = parentUrl.path + '/' + path
            page = pageUrl.page

, UrlProvider , templateObject.path. .

. , .

- ?

+3
2

http://code.google.com/appengine/docs/python/tools/webapp/running.html#URL_Mappings

, url

, ,

parama = , paramb =

class testPath (webapp.RequestHandler):   def get (self, parama, paramb): # 2 ,       self.response.out.write()       self.response.out.write(paramb)

.... # , , , "/test//" r " 'urlmapping'

application = webapp.WSGIApplication([                                   (r '/test/(.)/(.)', testPath),                                   ('/logout', LogoutHandler)                                   ],                                         = True)

, 'myapp.com/test/fruits/green

= paramb =

: D

+1

, ? http://code.google.com/appengine/docs/python/tools/webapp/running.html#URL_Mappings

http://test.com/browse?category=book&productid=A209 http://test.com/browse/book/A209

class BrowseHandler(webapp.RequestHandler):

def get(self, category, product_id):
    # Display product with given ID in the given category.


# Map URLs like /browse/(category)/(product_id) to BrowseHandler.
application = webapp.WSGIApplication([(r'/browse/(.*)/(.*)', BrowseHandler)
                                 ],
                                 debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
+1

Source: https://habr.com/ru/post/1747256/


All Articles