0

I have a url like

  1. www.example.com/events/A/
  2. www.example.com/events/B/

etc

I would like to change that to

  1. www.example.com/portal/A/ and likewise.

This can be achieved at Apache level, but can I do it in django using some middleware or something.

I have tried using RedirectView like this

url(r'events/', RedirectView.as_view(url='/portal/')),

But that just stops at /portal and anything after the events/ is ignored.

Is there a way to do this in django

Prabhakar Shanmugam
  • 5,634
  • 6
  • 25
  • 36
  • this will help http://stackoverflow.com/questions/31661044/how-to-redirect-url-pattern-with-variables-from-urls-py-in-django?noredirect=1&lq=1 – Dmitry Shilyaev Jan 30 '17 at 07:18

1 Answers1

0

So you are setting up redirects for old url's and to moving them to new one. There are several places you can do it. I.e. web server level, app level. One django way to do this is to setup redirect in your old controller and move all the logic to the new view.

from django.core.urlresolvers import reverse
from django.shortcuts import redirect, render_to_response


def event_view(request, event_identifier):
    return redirect(reverse('portal-view', kwargs={'portal_identifier': event_identifier}), permanent=True)


def portal_view(request, portal_identifier):
    return render_to_response('portal.html', {})

Don't forget to make it permanent redirect

Bipul Jain
  • 4,523
  • 3
  • 23
  • 26
  • Bipul thank you for your reply. In my case event_view is available. Since my urls are events/A, events/B etc my views are A(), B() etc. And there are like 100s of them and I will have to do the redirect of reverse at each view. Is there a middleware or something that can change www.example.com/events/A to www.example.com/portal/A etc – Prabhakar Shanmugam Jan 30 '17 at 06:40
  • You are not following dry principle here. You will have do some cleanup or lot of testing. Anyways take a look at this . http://stackoverflow.com/questions/31661044/how-to-redirect-url-pattern-with-variables-from-urls-py-in-django – Bipul Jain Jan 30 '17 at 06:48