0

So I have this small simple problem that I can't really find a solution to.

I would like to redirect all links on the form r'^blogg/ to r'^blog/'. I would think there is a solution similar to the accepted answer in this post here, but it doesn't work that well. Note that the blog-application has many sub-url-patterns, so a solution like RedirectView.as_view(url="/blog/") would not work.

In my main urls.py

urlpatterns = patterns('',
url(r'^blogg/', RedirectView.as_view(pattern_name="blog")),
url(r'^blog/', include("blog.urls", namespace="blog")),

The solution above returns HTTP 410 (Gone) for all sub-urls of blogg. I suspect this is due to the missing url argument in RedirectView.as_view().

Thanks in advance for all answers!

Community
  • 1
  • 1
Martin Hallén
  • 1,492
  • 1
  • 12
  • 27

2 Answers2

2

You won't be able to do this entirely in urls.py. But a simple view function could work:

url(r'^blogg/(?P<rest>.+)$', views.redirect_prefix),


from django.shortcuts import redirect

def redirect_prefix(request, rest):
    return redirect('/blog/{}'.format(rest))
Alasdair
  • 298,606
  • 55
  • 578
  • 516
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

What about

urlpatterns = patterns('',
url(r'^blogg/',include("blog.urls", namespace="ignoreme") ),
url(r'^blog/', include("blog.urls", namespace="blog")),

it both should strip the first part (blog/ or blogg/) from url and then manage the rest equally. And if you will use {% url "blog:this_view" %} then on next page your customer will be on the blog/ part anyway

gilhad
  • 609
  • 1
  • 5
  • 22
  • Thank you for the answer! Yes, I have considered this solution, but it's not ideal in my opinion (Because of some social plugins). Although, this is my fallback solution :) – Martin Hallén Jan 28 '16 at 18:59