Django url reverse: Non-reversible reg-exp portion: '(?='

匆匆过客 提交于 2019-12-24 15:06:08

问题


Django version: 1.5 (trunk)

I'm using a positive look-ahead assertion in url pattern A, which works fine by itself. But when I try to reverse url pattern B, which is completelly unrelated, I get:

ValueError: Non-reversible reg-exp portion: '(?='

Example urls:

url(r'^foo(?=bar)/', test, name= 'bla'),
url(r'bar/', test, name= 'bli'),

Triggering the error:

from django.core.urlresolvers import reverse
reverse('bli')

I found this related ticket, but didn't make me smarter sadly https://code.djangoproject.com/ticket/17492

Anyone can tell me what's wrong with the code?


回答1:


Your code is OK, the problem is, Django can't reverse every possible regular expression. Currently Django's implementation of regex normalizer can't handle at least two things: disjunction (|) and non-capturing (look-ahead, look-behind) patterns.

So, to solve your problem, just avoid using look-ahead in your URL patterns and you're good to go. It should be possible, after all, using plain regular expressions without all those funky extensions it is possible to represent any regular language.



来源:https://stackoverflow.com/questions/10570463/django-url-reverse-non-reversible-reg-exp-portion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!