How to reformat URLs to be more restful (from …/?id=123 to …/123)?

纵饮孤独 提交于 2019-12-10 10:04:45

问题


Currently I have pages accessed via:

www.foo.com/details.html?id=123

I'd like to make them more restful-like, such as by the following:

www.foo.com/details/123

I'm using Google App Engine. Currently the URL's are mapped in the html-mappings file:

 ('/details.html*', DetailsPage),

And then on the DetailsPage handler, it fetches the ID value via:

class DetailsPage(webapp.RequestHandler):
    def get(self):
        announcement_id = self.request.get("id")

How might I restructure this so that it can map the URL and a extract the ID via the other-formatted URL: www.foo.com/details/123

Thanks


回答1:


Rewrite your URL mapping like this:

('/details/(\d+)', DetailsPage),

(this requires that there be a trailing part of the URL that contains one or more digits and nothing else).

Then modify your DetailsPage::get() method to accept that id parameter, like:

class DetailsPage(webapp.RequestHandler):
    def get(self, announcement_id):
        # next line no longer needed
        # announcement_id = self.request.get("id") 

...and carry on with your code.



来源:https://stackoverflow.com/questions/4594602/how-to-reformat-urls-to-be-more-restful-from-id-123-to-123

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!