问题
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