Django URL Redirection

How can I redirect traffic that does not match any of my other URLs to the home page?

urls.py:

urlpatterns = patterns('', url(r'^$', 'macmonster.views.home'), #url(r'^macmon_home$', 'macmonster.views.home'), url(r'^macmon_output/$', 'macmonster.views.output'), url(r'^macmon_about/$', 'macmonster.views.about'), url(r'^.*$', 'macmonster.views.home'), ) 

As is, the last entry sends all the β€œother” traffic to the home page, but I want to redirect through HTTP 301 or 302 .

+93
python django django-urls
Feb 19 '13 at 13:54
source share
5 answers

You can try Class Based View called RedirectView

 from django.views.generic.base import RedirectView urlpatterns = patterns('', url(r'^$', 'macmonster.views.home'), #url(r'^macmon_home$', 'macmonster.views.home'), url(r'^macmon_output/$', 'macmonster.views.output'), url(r'^macmon_about/$', 'macmonster.views.about'), url(r'^.*$', RedirectView.as_view(url='<url_to_home_view>', permanent=False), name='index') ) 

Note that as a url in <url_to_home_view> you need to specify a URL.

permanent=False will return HTTP 302, and permanent=True will return HTTP 301.

Alternatively you can use django.shortcuts.redirect

+165
Feb 19 '13 at 13:57
source share

In Django 1.8, I did just that.

 from django.views.generic.base import RedirectView url(r'^$', views.comingSoon, name='homepage'), # whatever urls you might have in here # make sure the 'catch-all' url is placed last url(r'^.*$', RedirectView.as_view(pattern_name='homepage', permanent=False)) 

Instead of using url you can use pattern_name , which is a bit dry, and will provide you with a change in your URL, you will also not have to change the redirection.

+32
Jul 08 '15 at 20:22
source share

If you are stuck on django 1.2, like me, and RedirectView doesn't exist, another route-oriented way to add a redirect mapping uses:

 (r'^match_rules/$', 'django.views.generic.simple.redirect_to', {'url': '/new_url'}), 

You can also redirect everything to a match. This is useful when changing the application folder, but to save bookmarks:

 (r'^match_folder/(?P<path>.*)', 'django.views.generic.simple.redirect_to', {'url': '/new_folder/%(path)s'}), 

This is preferable to django.shortcuts.redirect if you are only trying to change your URL routing and don't have access to .htaccess, etc. (I on Appengine and app.yaml do not allow URL redirection with a level like .htaccess).

+10
Mar 03 '14 at 22:19
source share

Another way to do this is to use the HttpResponsePermanentRedirect like this:

In view.py

 def url_redirect(request): return HttpResponsePermanentRedirect("/new_url/") 

In url.py

 url(r'^old_url/$', "website.views.url_redirect", name="url-redirect"), 
+8
Feb 26 '15 at 12:32
source share

Other methods work well, but you can also use the good old django.shortcut.redirect .

The code below is taken from this answer r

In Django 2.x,

 from django.contrib import admin from django.shortcuts import redirect from django.urls import path, include urlpatterns = [ # this example uses named URL 'hola-home' from app named hola # for more redirect usage options: https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/ path('', lambda request: redirect('hola/', permanent=False)), path('hola/', include("hola.urls")), path('admin/', admin.site.urls), ] 
+2
Jul 27 '19 at 10:08
source share



All Articles