2
TLDR; {%url%}標籤有效。 r'(?i)^ ... $'url路徑工作。他們不一起工作?Django:NoReverseMatch帶有網址標記和不區分大小寫的網址
我想用反向URL解析(通過URL模板標籤),但不管這似乎不兼容不區分大小寫的URL的正則表達式的原因(即正則表達式前綴「(我)」) 。要弄清這個作品:
urls.py:
...
urlpatterns = patterns('',
url(r'^$', home, name='home'),
...
)
...
base_path.html
<a href="{% url home %}">Users</a>
但是這會導致NoReverseMatch錯誤:
urls.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路徑重現了這種行爲 - 通過刪除「(?i)」前綴(儘管我們確實需要不區分大小寫)來修復這些問題。
django的哪個版本是這樣的? – melwil
@melwil 1.4.3,請參閱錯誤說明中的信息:) – 0x24a537r9
嘗試在'^'之後放置'(?i)'。 – melwil