webapp2(GAE)上的URL模式映射

时间:2013-07-01 20:39:39

标签: python google-app-engine url webapp2

我对 webapp2 路由感到奇怪,这就是为什么, 在下面的示例中,我的网站上有新闻映射的透视图。

问题是,为什么在某些键中 strict_slash = True 运行良好且(生成301)而在另一些键中根本不起作用(404)?

from webapp2 import WSGIApplication, Route as url
from webapp2_extras.routes import (PathPrefixRoute as patterns, 
                                   RedirectRoute as rurl)

from .views import (MainHandler)
from .views_news import (NewsAllHandler, NewsAnnualHandler, NewsMonthlyHandler, 
                         NewsDailyHandler, NewsTagsHandler, NewsTagHandler, 
                         NewsHandler)

app = WSGIApplication([
    url('/', MainHandler, 'main'),
    patterns('/news', [
        rurl('/', NewsAllHandler, 'news_all', 
             strict_slash=True), # here wont work /news isn't /news/ — get 404
        patterns('/tags', [
            rurl('/', NewsTagsHandler, 'news_tags', 
                 strict_slash=True), # works well /tags 301 redirects to /tags/
            rurl('/<tag:[\w\d-]+>/', NewsTagHandler, 'news_tag', 
                 strict_slash=True),
        ]),
        patterns('/<year:\d{4}>', [
            rurl('/', NewsAnnualHandler, 'news_year', 
                 strict_slash=True), # works well /d{4} 301 redirects to /d{4}/
            rurl('/<month:\d{2}>/', NewsMonthlyHandler, 'news_month', 
                 strict_slash=True),
            rurl('/<month:\d{2}>/<day:\d{2}>/', NewsDailyHandler, 'news_day', 
                 strict_slash=True),
            rurl('/<month:\d{2}>/<day:\d{2}>/<slug:[\w\d-]+>/', NewsHandler, 'news', 
                 strict_slash=True),
        ]),
    ]),
], debug=True)

我错过了什么吗? 拜托,帮助我,这让我感到非常震惊!

1 个答案:

答案 0 :(得分:0)

现在我在patterns('/news', […])之后使用 RedirectRoute /news映射到/news/

rurl('/news', redirect_to='/news/'),