I need a django regex that will actually work for the url router to do the following:
我需要一个django正则表达式,它实际上适用于url路由器执行以下操作:
Match everything that does not contain "/api" in the route.
匹配路线中不包含“/ api”的所有内容。
The following does not work because django can't reverse (?!
以下不起作用,因为django不能反转(?!
r'^(?!api)
3 个解决方案
#1
6
The usual way to go about this would be to order the route declarations so that the catch-all route is shadowed by the /api
route, i.e.:
通常的方法是订购路由声明,以便全部路由被/ api路由遮蔽,即:
urlpatterns = patterns('',
url(r'^api/', include('api.urls')),
url(r'^other/', 'views.other', name='other'),
url(r'^.*$', 'views.catchall', name='catch-all'),
)
Alternatively, if for some reason you really need to skip some routes but cannot do it with the set of regexes supported by Django, you could define a custom pattern matcher class:
或者,如果由于某种原因你真的需要跳过一些路由但不能用Django支持的一组正则表达式来做,你可以定义一个自定义模式匹配器类:
from django.core.urlresolvers import RegexURLPattern
class NoAPIPattern(RegexURLPattern):
def resolve(self, path):
if not path.startswith('api'):
return super(NoAPIPattern, self).resolve(path)
urlpatterns = patterns('',
url(r'^other/', 'views.other', name='other'),
NoAPIPattern(r'^.*$', 'views.catchall', name='catch-all'),
)
#2
1
Use a negative look behind like so:
像这样使用负面外观:
r'^(?!/api).*$'
This link explains how to do that:
此链接说明了如何执行此操作:
http://www.codinghorror.com/blog/2005/10/excluding-matches-with-regular-expressions.html
#3
0
I had this problem integrating django with react router, I check this link to fix it.
我有这个问题集成django与反应路由器,我检查此链接来修复它。
decoupled frontend and backend with Django, webpack, reactjs, react-router
与Django,webpack,reactjs,react-router解耦前端和后端
#1
6
The usual way to go about this would be to order the route declarations so that the catch-all route is shadowed by the /api
route, i.e.:
通常的方法是订购路由声明,以便全部路由被/ api路由遮蔽,即:
urlpatterns = patterns('',
url(r'^api/', include('api.urls')),
url(r'^other/', 'views.other', name='other'),
url(r'^.*$', 'views.catchall', name='catch-all'),
)
Alternatively, if for some reason you really need to skip some routes but cannot do it with the set of regexes supported by Django, you could define a custom pattern matcher class:
或者,如果由于某种原因你真的需要跳过一些路由但不能用Django支持的一组正则表达式来做,你可以定义一个自定义模式匹配器类:
from django.core.urlresolvers import RegexURLPattern
class NoAPIPattern(RegexURLPattern):
def resolve(self, path):
if not path.startswith('api'):
return super(NoAPIPattern, self).resolve(path)
urlpatterns = patterns('',
url(r'^other/', 'views.other', name='other'),
NoAPIPattern(r'^.*$', 'views.catchall', name='catch-all'),
)
#2
1
Use a negative look behind like so:
像这样使用负面外观:
r'^(?!/api).*$'
This link explains how to do that:
此链接说明了如何执行此操作:
http://www.codinghorror.com/blog/2005/10/excluding-matches-with-regular-expressions.html
#3
0
I had this problem integrating django with react router, I check this link to fix it.
我有这个问题集成django与反应路由器,我检查此链接来修复它。
decoupled frontend and backend with Django, webpack, reactjs, react-router
与Django,webpack,reactjs,react-router解耦前端和后端