[Django]-Alias for app URI in Django project urls.py

6👍

You should try something like that:

from django.views.generic import RedirectView

urlpatterns=[
    url(r'^birds/', include('birds.urls')),
    url(r'^b/(?P<path>.*)$', RedirectView.as_view(url='/birds/%(path)s')),
]

0👍

You can make a custom middleware to handle the redirects for you

class BirdMiddleware(MiddlewareMixin, object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        if '/b/' in request.path:
             return HttpResponseRedirect(request.path.replace('/b/', '/birds/'))
        return None

The implementation may need a bit of work but the actual method stands, check the current path and if the /b/ is present, redirect it to your required destination.

Otherwise, you could specify a redirect view by iterating over every url in birds but this would get messy for urls in birds that are a namespace to other urls.

👤Sayse

Leave a comment