221๐
If you are on Django 1.4 or 1.5, you can do this:
from django.core.urlresolvers import reverse_lazy
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-page/$', RedirectView.as_view(url=reverse_lazy('my_named_pattern'), permanent=False)),
...
If you are on Django 1.6 or above, you can do this:
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-page/$', RedirectView.as_view(pattern_name='my_named_pattern', permanent=False)),
...
In Django 1.9, the default value of permanent
has changed from True to False. Because of this, if you donโt specify the permanent
keyword argument, you may see this warning:
RemovedInDjango19Warning: Default value of โRedirectView.permanentโ will change from True to False in Django 1.9. Set an explicit value to silence this warning.
41๐
This works for me.
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-page/$', RedirectView.as_view(url='/')),
...
In above example '/'
means it will redirect to index page,
where you can add any url patterns also.
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
- [Django]-How to check if ManyToMany field is not empty?
- [Django]-Github issues api 401, why? (django)
33๐
for django v2+
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's 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),
]
- [Django]-Django set default form values
- [Django]-Paginating the results of a Django forms POST request
- [Django]-Sending HTML email in django
11๐
I was trying to redirect all 404s to the home page and the following worked great:
from django.views.generic import RedirectView
# under urlpatterns, added:
url(r'^.*/$', RedirectView.as_view(url='/home/')),
url(r'^$', RedirectView.as_view(url='/home/')),
- [Django]-Django South โ table already exists
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'
- [Django]-How to log all sql queries in Django?
9๐
This way is supported in older versions of django if you cannot support RedirectView
In view.py
def url_redirect(request):
return HttpResponseRedirect("/new_url/")
In the url.py
url(r'^old_url/$', "website.views.url_redirect", name="url-redirect"),
You can make it permanent by using HttpResponsePermanentRedirect
- [Django]-Phpmyadmin logs out after 1440 secs
- [Django]-Django storages: Import Error โ no module named storages
- [Django]-Django admin and MongoDB, possible at all?
1๐
You could do straight on the urls.py just doing something like:
url(r'^story/(?P<pk>\d+)/',
lambda request, pk: HttpResponsePermanentRedirect('/new_story/{pk}'.format(pk=pk)))
Just ensure that you have the new URL ready to receive the redirect!!
Also, pay attention to the kind of redirect, in the example Iโm using Permanent Redirect
- [Django]-Django TemplateSyntaxError โ 'staticfiles' is not a registered tag library
- [Django]-Filtering using viewsets in django rest framework
- [Django]-When saving, how can you check if a field has changed?