Agus Makmun

I'm Programmer for Python & Django. I've made quite a few web apps, especially on Django.

Custom redirect urls django

20 Apr 2016 » python, django

Example in this problem we need redirect the url http://localhost:8000/a/b/C/123/4/5/ to http://localhost:8000/abC12345 without / slash.

1. In your

from django.http import HttpResponse
from django.views.generic.base import RedirectView
from django.core.urlresolvers import reverse

class UserRedirectView(RedirectView):
    permanent = False
    def get_redirect_url(self, pk):
        pk = ''.join(str(pk).split('/'))
        return reverse('pool_fix_page', kwargs={'pk': pk})

def pool_fix(request, pk):
    return HttpResponse("You're looking at question %s." % pk)

2. In your

from django.conf.urls import url
from . import views

urlpatterns = [
    # first view the pool to doing redirection
    url(r'^pool/(?P<pk>[0-9a-zA-Z\/]+)/$', views.UserRedirectView.as_view(), name='pool'),

    # allow decimal and words only.
    url(r'^pool/(?P<pk>[\d\w_]+)$', views.pool_fix, name='pool_fix_page'),