Google App Engine 中的友好 URL

发布于 2024-09-02 22:44:02 字数 1469 浏览 5 评论 0原文

我正在尝试在 GAE 和 Python 中构建一个友好的 URL 查找方法。有人这样做过吗?

我已经可以使用了,但感觉很不稳定。它的工作原理是获取路径(比方说 /foo/bar/)并将其拆分为一个列表。获取列表中的最后一个元素并查询数据库以查找匹配项。现在有些头痛了。

如果有多个“栏”,因为可能有另一个“栏”但在不同的页面下,如何区分它们?到目前为止,我的解决方案是迭代“bar”的结果并查看其父页面的引用属性。父级“foo”也可能出现多次,所以现在我们必须对其进行迭代。不难看出,这个云很容易就加起来大量的 for 循环。

更糟糕的是,一个页面可以使用多种语言,并且 url 需要与给定语言匹配。

我当前的数据存储设置是这样的:

## 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)

使用 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 列表中最后一项匹配的所有内容。迭代它并获取所有父级并尝试匹配它。

截至目前,它仅适用于一个根页面和一个子页面。 我不知道如何使其更加动态并支持多个级别。

有没有人自己写过或者有什么好的建议?

I'm trying to build a Friendly URL lookup method in GAE and Python. Have anyone done this?

I've got it working but it feels VERY shaky. How it works is that it takes the path (let's say /foo/bar/) and splits it to a list. Takes the last element in the list and query the database for a match. Now comes some headaches.

If there is more that one 'bar' since there could be another 'bar' but under a different page, how to tell them apart? As of now, my solution is to iterate over the result of 'bar' and look at a reference property to it's parent page. The parent 'foo' could also occur more then once so now we have to iterate over that to. It isn't hard to see that this cloud easily add up to a large number of for loops.

Just to make it a bit worse one page could be able in more then one language and the url need to match the give language.

My current datastore setup is this:

## 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 fetch a 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

What it does is, it fetches from UrlProvider everything that matches the last item in the templateObject.path list. Iterates over it and fetches all parents and trying to match it.

As of now it only works for one root page and one subpage.
I don't know how to make it more dynamic and support multiple levels.

Have anyone written their own or have any good suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空气里的味道 2024-09-09 22:44:02

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

查看 url 映射部分,

这是完整的代码,但我希望您明白

这是我的请求处理程序 param = 我的第一个参数,paramb = 我的第二个参数

类 testPath(webapp.RequestHandler):
def get(self,parama,paramb): # 注意 2 个额外的参数,不仅仅是 self
self.response.out.write(参数)
self.response.out.write(paramb)

.... # 下面你可以看到我映射了任何请求“/test//”的内容,注意“urlmapping”之前的“r”

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

所以当我去 'myapp.com/test/fruits/green

parama =fruit
参数=绿色

:D

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

Check out the section that says url mappings

this is complete code but I hope you get the idea

this is my request handler parama = my first param, paramb = my second param

class testPath(webapp.RequestHandler):
def get(self,parama,paramb): # notice the 2 extra params, not just self
self.response.out.write(parama)
self.response.out.write(paramb)

.... # below you see i mapped anything that request "/test// notice the "r" before the 'urlmapping'

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

so when i go to 'myapp.com/test/fruits/green

parama = fruit
paramb = green

:D

鹿港巷口少年归 2024-09-09 22:44:02

这是您要找的吗? 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()

Is this what you're looking for? http://code.google.com/appengine/docs/python/tools/webapp/running.html#URL_Mappings

Instead of http://test.com/browse?category=book&productid=A209 you can have 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文