Django URL pattern match (all but pattern)

I need a django regex that will really work for the url router to do the following:

Match everything that does not contain "/ api" in the route.

Below does not work, because django cannot undo (?!

r'^(?!api)
+4
source share
3 answers

The usual way to solve this problem would be to arrange route declarations so that the all-all route is obscured by tracing /api, that is:

urlpatterns = patterns('', 
    url(r'^api/', include('api.urls')),
    url(r'^other/', 'views.other', name='other'),
    url(r'^.*$', 'views.catchall', name='catch-all'), 
)

, - , , 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'),
)
+6

All Articles