Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored January 03, 2006
2  django/contrib/redirects/middleware.py
@@ -21,7 +21,7 @@ def process_response(self, request, response):
21 21
         if r is not None:
22 22
             if r == '':
23 23
                 return httpwrappers.HttpResponseGone()
24  
-            return httpwrappers.HttpResponseRedirect(r.new_path)
  24
+            return httpwrappers.HttpResponsePermanentRedirect(r.new_path)
25 25
 
26 26
         # No redirect was found. Return the response.
27 27
         return response
2  django/middleware/common.py
@@ -46,7 +46,7 @@ def process_request(self, request):
46 46
                 newurl = new_url[1]
47 47
             if request.GET:
48 48
                 newurl += '?' + request.GET.urlencode()
49  
-            return httpwrappers.HttpResponseRedirect(newurl)
  49
+            return httpwrappers.HttpResponsePermanentRedirect(newurl)
50 50
 
51 51
         return None
52 52
 
6  django/utils/httpwrappers.py
@@ -212,6 +212,12 @@ def __init__(self, redirect_to):
212 212
         self['Location'] = redirect_to
213 213
         self.status_code = 302
214 214
 
  215
+class HttpResponsePermanentRedirect(HttpResponse):
  216
+    def __init__(self, redirect_to):
  217
+        HttpResponse.__init__(self)
  218
+        self['Location'] = redirect_to
  219
+        self.status_code = 301
  220
+
215 221
 class HttpResponseNotModified(HttpResponse):
216 222
     def __init__(self):
217 223
         HttpResponse.__init__(self)
20  django/views/generic/simple.py
... ...
@@ -1,28 +1,28 @@
1 1
 from django.core.extensions import DjangoContext, render_to_response
2  
-from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, HttpResponseGone
  2
+from django.utils.httpwrappers import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
3 3
 
4 4
 def direct_to_template(request, template, **kwargs):
5 5
     """
6  
-    Render a given template with any extra URL parameters in the context as 
  6
+    Render a given template with any extra URL parameters in the context as
7 7
     ``{{ params }}``.
8 8
     """
9 9
     return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request))
10  
-    
  10
+
11 11
 def redirect_to(request, url, **kwargs):
12 12
     """
13  
-    Redirect to a given URL.  
14  
-    
15  
-    The given url may contain dict-style string formatting which will be
  13
+    Redirect to a given URL.
  14
+
  15
+    The given url may contain dict-style string formatting, which will be
16 16
     interpolated against the params in the URL.  For example, to redirect from
17  
-    ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following urlpattern::
  17
+    ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf::
18 18
 
19 19
         urlpatterns = patterns('',
20 20
             ('^foo/(?p<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
21 21
         )
22  
-        
  22
+
23 23
     If the given url is ``None``, a HttpResponseGone (410) will be issued.
24 24
     """
25 25
     if url is not None:
26  
-        return HttpResponseRedirect(url % kwargs)
  26
+        return HttpResponsePermanentRedirect(url % kwargs)
27 27
     else:
28  
-        return HttpResponseGone()
  28
+        return HttpResponseGone()
9  docs/request_response.txt
@@ -357,7 +357,14 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in
357 357
 ``HttpResponseRedirect``
358 358
     The constructor takes a single argument -- the path to redirect to. This
359 359
     can be a fully qualified URL (e.g. ``"http://www.yahoo.com/search/"``) or an
360  
-    absolute URL with no domain (e.g. ``"/search/"``).
  360
+    absolute URL with no domain (e.g. ``"/search/"``). Note that this returns
  361
+    an HTTP status code 302.
  362
+
  363
+``HttpResponsePermanentRedirect``
  364
+    **New in Django development version.***
  365
+
  366
+    Like ``HttpResponseRedirect``, but it returns a permanent redirect (HTTP
  367
+    status code 301) instead of a "found" redirect (status code 302).
361 368
 
362 369
 ``HttpResponseNotModified``
363 370
     The constructor doesn't take any arguments. Use this to designate that a

0 notes on commit 3234a93

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