2

TLDR;{% url %} 标签有效。r'(?i)^...$' url 路径有效。他们不合作?

我想使用反向 url 解析(通过 url 模板标签),但无论出于何种原因,它似乎与不区分大小写的 url 正则表达式(即前缀为“(?i)”的正则表达式)不兼容。为了澄清这个工作:

网址.py:

...
urlpatterns = patterns('',
    url(r'^$', home, name='home'),
    ...
)
...

base_path.html

<a href="{% url home %}">Users</a>

但这会导致 NoReverseMatch 错误:

网址.py:

...
urlpatterns = patterns('',
    url(r'(?i)^$', home, name='home'),
    ...
)
...

具体来说,我得到:

NoReverseMatch at /p/blah/users/
  Reverse for 'home' with arguments '()' and keyword arguments '{}' not found.
  Request Method:   GET
  Request URL:  http://localhost:8000/p/blah/users/
  Django Version:   1.4.3
  Exception Type:   NoReverseMatch
  Exception Value:  
    Reverse for 'home' with arguments '()' and keyword arguments '{}' not found.
  Exception Location: .../local/lib/python2.7/site-packages/django/template/defaulttags.py in render, line 424
  Python Executable:    .../bin/python
  Python Version:   2.7.3

任何想法A)为什么会发生这种情况,和/或B)url模板标签或不区分大小写的url正则表达式的解决方法?我已经用我们的许多其他 url 路径重现了这种行为——所有这些都通过删除“(?i)”前缀来修复(尽管我们真的想要不区分大小写)。

4

1 回答 1

4

Django url 模式似乎需要您将 regexflags 放在锚点之后^

url(r'^(?i)$', home, name='home'),
于 2013-05-27T08:05:23.203 回答