Django URL反向:不可逆的正则表达式部分:'(?=' [英] Django url reverse: Non-reversible reg-exp portion: '(?='

查看:142
本文介绍了Django URL反向:不可逆的正则表达式部分:'(?='的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django版本:1.5(trunk)

Django version: 1.5 (trunk)

我在网址格式A中使用了肯定的前瞻性断言,它本身可以很好地工作。但是当我尝试反向完全不相关的网址格式B时,我得到:

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: '(?='

示例网址:

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

触发错误:

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

我找到了这张相关的票,但可悲的是,这并没有使我更聪明
https://code.djangoproject.com/ticket/17492

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?

推荐答案

您的代码还可以,问题是Django无法反转所有可能的正则表达式,目前Django的regex normalizer实现无法处理至少有两件事:析取( | )和非

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.

因此,要解决您的问题,只需避免在URL模式中使用前瞻性即可,走。毕竟,应该可以使用不带所有那些时髦扩展的普通正则表达式来表示任何正则语言。

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.

这篇关于Django URL反向:不可逆的正则表达式部分:'(?='的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆