How can I make a trailing slash optional for included module urls

1k Views Asked by At

I have a third party django app with it's own urls but they require a trailing slash. How can I support both with and without the slash?

1

There are 1 best solutions below

2
On
import re

from django.conf.urls import url, include

def optional_trailing_slash(urls):
   for url in urls[0].urlpatterns:
       url.regex = re.compile(url.regex.pattern.replace('/$', '/?$'))
   return urls

urlpatterns = [
   url(r'^', optional_trailing_slash(include('third_party_app.urls'))),
]