256π
Itβs simple:
from django.http import HttpResponseRedirect
def myview(request):
...
return HttpResponseRedirect("/path/")
More info in the official Django docs
Update: Django 1.0
There is apparently a better way of doing this in Django now using generic views
.
Example β
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^one/$', redirect_to, {'url': '/another/'}),
#etc...
)
There is more in the generic views documentation.
Credit β Carles BarrobΓ©s.
Update #2: Django 1.3+
In Django 1.5 redirect_to no longer exists and has been replaced by RedirectView. Credit to Yonatan
from django.views.generic import RedirectView
urlpatterns = patterns('',
(r'^one/$', RedirectView.as_view(url='/another/')),
)
114π
Depending on what you want (i.e. if you do not want to do any additional pre-processing), it is simpler to just use Djangoβs redirect_to
generic view:
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^one/$', redirect_to, {'url': '/another/'}),
#etc...
)
See documentation for more advanced examples.
For Django 1.3+ use:
from django.views.generic import RedirectView
urlpatterns = patterns('',
(r'^one/$', RedirectView.as_view(url='/another/')),
)
- [Django]-Django β makemigrations β No changes detected
- [Django]-Celery. Decrease number of processes
- [Django]-How do I do a not equal in Django queryset filtering?
38π
Thereβs actually a simpler way than having a view for each redirect β you can do it directly in urls.py
:
from django.http import HttpResponsePermanentRedirect
urlpatterns = patterns(
'',
# ...normal patterns here...
(r'^bad-old-link\.php',
lambda request: HttpResponsePermanentRedirect('/nice-link')),
)
A target can be a callable as well as a string, which is what Iβm using here.
- [Django]-How to iterate through dictionary in a dictionary in django template?
- [Django]-Django change default runserver port
- [Django]-Django: Filter a Queryset made of unions not working
28π
Since Django 1.1, you can also use the simpler redirect shortcut:
from django.shortcuts import redirect
def myview(request):
return redirect('/path')
It also takes an optional permanent=True keyword argument.
- [Django]-How to revert the last migration?
- [Django]-How does the get_or_create function in Django return two values?
- [Django]-How can I check the size of a collection within a Django template?
14π
If you want to redirect a whole subfolder, the url
argument in RedirectView is actually interpolated, so you can do something like this in urls.py
:
from django.conf.urls.defaults import url
from django.views.generic import RedirectView
urlpatterns = [
url(r'^old/(?P<path>.*)$', RedirectView.as_view(url='/new_path/%(path)s')),
]
The ?P<path>
you capture will be fed into RedirectView
. This captured variable will then be replaced in the url
argument you gave, giving us /new_path/yay/mypath
if your original path was /old/yay/mypath
.
You can also do β¦.as_view(url='β¦', query_string=True)
if you want to copy the query string over as well.
- [Django]-How can I upgrade specific packages using pip and a requirements file?
- [Django]-Django: How can I create a multiple select form?
- [Django]-Django Footer and header on each page with {% extends }
10π
With Django version 1.3, the class based approach is:
from django.conf.urls.defaults import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^some-url/$', RedirectView.as_view(url='/redirect-url/'), name='some_redirect'),
)
This example lives in in urls.py
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
- [Django]-Invalid http_host header
6π
Beware. I did this on a development server and wanted to change it later.
I had to clear my caches to change it. In order to avoid this head-scratching in the future, I was able to make it temporary like so:
from django.views.generic import RedirectView
url(r'^source$', RedirectView.as_view(permanent=False,
url='/dest/')),
- [Django]-Multiple Database Config in Django 1.2
- [Django]-No module named MySQLdb
- [Django]-Django β Circular model import issue
2π
page_path = define in urls.py
def deletePolls(request):
pollId = deletePool(request.GET['id'])
return HttpResponseRedirect("/page_path/")
- [Django]-How do I get the class of a object within a Django template?
- [Django]-Favorite Django Tips & Features?
- [Django]-How to understand lazy function in Django utils functional module
1π
You can do this in the Admin section. Itβs explained in the documentation.
https://docs.djangoproject.com/en/dev/ref/contrib/redirects/
- [Django]-Django β How to pass several arguments to the url template tag
- [Django]-Get user profile in django
- [Django]-Google Static Maps URL length limit
0π
This should work in most versions of django, I am using it in 1.6.5:
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
urlpatterns = patterns('',
....
url(r'^(?P<location_id>\d+)/$', lambda x, location_id: HttpResponseRedirect(reverse('dailyreport_location', args=[location_id])), name='location_stats_redirect'),
....
)
You can still use the name of the url pattern instead of a hard coded url with this solution. The location_id parameter from the url is passed down to the lambda function.
- [Django]-How to completely dump the data for Django-CMS
- [Django]-Django fix Admin plural
- [Django]-Django Rest Framework custom response message