Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #1117 -- Added HttpResponsePermanentRedirect

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1816 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3234a932b2054e30604d29f93e65180fa7c3fc05 1 parent 6cca806
@adrianholovaty adrianholovaty authored
View
2  django/contrib/redirects/middleware.py
@@ -21,7 +21,7 @@ def process_response(self, request, response):
if r is not None:
if r == '':
return httpwrappers.HttpResponseGone()
- return httpwrappers.HttpResponseRedirect(r.new_path)
+ return httpwrappers.HttpResponsePermanentRedirect(r.new_path)
# No redirect was found. Return the response.
return response
View
2  django/middleware/common.py
@@ -46,7 +46,7 @@ def process_request(self, request):
newurl = new_url[1]
if request.GET:
newurl += '?' + request.GET.urlencode()
- return httpwrappers.HttpResponseRedirect(newurl)
+ return httpwrappers.HttpResponsePermanentRedirect(newurl)
return None
View
6 django/utils/httpwrappers.py
@@ -212,6 +212,12 @@ def __init__(self, redirect_to):
self['Location'] = redirect_to
self.status_code = 302
+class HttpResponsePermanentRedirect(HttpResponse):
+ def __init__(self, redirect_to):
+ HttpResponse.__init__(self)
+ self['Location'] = redirect_to
+ self.status_code = 301
+
class HttpResponseNotModified(HttpResponse):
def __init__(self):
HttpResponse.__init__(self)
View
20 django/views/generic/simple.py
@@ -1,28 +1,28 @@
from django.core.extensions import DjangoContext, render_to_response
-from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, HttpResponseGone
+from django.utils.httpwrappers import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
def direct_to_template(request, template, **kwargs):
"""
- Render a given template with any extra URL parameters in the context as
+ Render a given template with any extra URL parameters in the context as
``{{ params }}``.
"""
return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request))
-
+
def redirect_to(request, url, **kwargs):
"""
- Redirect to a given URL.
-
- The given url may contain dict-style string formatting which will be
+ Redirect to a given URL.
+
+ The given url may contain dict-style string formatting, which will be
interpolated against the params in the URL. For example, to redirect from
- ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following urlpattern::
+ ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf::
urlpatterns = patterns('',
('^foo/(?p<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
)
-
+
If the given url is ``None``, a HttpResponseGone (410) will be issued.
"""
if url is not None:
- return HttpResponseRedirect(url % kwargs)
+ return HttpResponsePermanentRedirect(url % kwargs)
else:
- return HttpResponseGone()
+ return HttpResponseGone()
View
9 docs/request_response.txt
@@ -357,7 +357,14 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in
``HttpResponseRedirect``
The constructor takes a single argument -- the path to redirect to. This
can be a fully qualified URL (e.g. ``"http://www.yahoo.com/search/"``) or an
- absolute URL with no domain (e.g. ``"/search/"``).
+ absolute URL with no domain (e.g. ``"/search/"``). Note that this returns
+ an HTTP status code 302.
+
+``HttpResponsePermanentRedirect``
+ **New in Django development version.***
+
+ Like ``HttpResponseRedirect``, but it returns a permanent redirect (HTTP
+ status code 301) instead of a "found" redirect (status code 302).
``HttpResponseNotModified``
The constructor doesn't take any arguments. Use this to designate that a

0 comments on commit 3234a93

Please sign in to comment.
Something went wrong with that request. Please try again.