django url tag:渲染时捕获NoReverseMatch

时间:2011-09-29 15:33:33

标签: django django-urls

#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 = ''):
    ...



如果我在地址栏或模板/places/types/10/中手动打开网址{% url url_places %},则可以使用 但在模板{% url url_places types=10 %}中引发异常:

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

为什么以及如何解决?

1 个答案:

答案 0 :(得分:0)

相关问题