【问题标题】:django url tag: Caught NoReverseMatch while renderingdjango url 标签:Caught NoReverseMatch while rendering
【发布时间】:2011-09-29 15:33:33
【问题描述】:
#urls.py
places_url_regex = ur'''
    ^places
    (
    (?:/types/(?P<types>\d+(?:-\d+)*))
    |
    (?:/cuisines/(?P<cuisines>\d+(?:-\d+)*))
    |
    (?:/avg-bills/(?P<avg_bills>\d+(?:-\d+)*))
    |
    (?:/cities/(?P<cities>\d+(?:-\d+)*))
    |
    (?:/sectors/(?P<sectors>\d+(?:-\d+)*))
    |
    (?:/streets/(?P<streets>\d+(?:-\d+)*))
    |
    (?:/special/(?P<special>[cabins|calian|children\-animation|children\-room|parking|summer\-verandah|vip-room|wifi|smokers\-room]+))
    |
    (?:/with/(?P<w>[discounts]+))
    |
    (?:/page/(?P<page>\d+))
    )*
    /$
'''
places_url_regex = re.sub( ur'\s+', '', places_url_regex )
urlpatterns = patterns( '',
    url( places_url_regex, 'app.places.views.places', name = 'url_places' ),
)
#views.py
def places(request, page = 1, special = '', types = '', cuisines = '', avg_bills = '', cities = '', sectors = '', streets = '', w = ''):
    ...



如果我在地址栏或模板{% url url_places %} 中手动打开 url /places/types/10/ 它是有效的, 但在模板 {% url url_places types=10 %} 中引发异常:

Caught NoReverseMatch while rendering: Reverse for 'url_places' with arguments '()' and keyword arguments '{'types': 10}' not found.

为什么以及如何解决?

【问题讨论】:

    标签: django django-urls


    【解决方案1】:

    【讨论】:

    • Could not parse the remainder: '=10' from '"types"=10'
    • 这是有道理的。有趣的是,您为什么不使用传统的 url 方法?
    • 常规做法是什么?
    • 这很有趣。如果我将正则表达式更改为:url( ur'^places/$', 'app.places.views.places', name = 'url_places' ), url( ur'^places(?:/types/(?P&lt;types&gt;\d+(?:-\d+)*))/$', 'app.places.views.places' ), url 标签有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2011-07-01
    • 2010-11-07
    • 1970-01-01
    • 2014-07-17
    • 2013-09-26
    相关资源
    最近更新 更多