Fixed #1117 -- Added HttpResponsePermanentRedirect

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1816 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-01-03 23:57:14 +00:00
parent 6cca806943
commit 3234a932b2
5 changed files with 26 additions and 13 deletions

View File

@ -21,7 +21,7 @@ class RedirectFallbackMiddleware:
if r is not None: if r is not None:
if r == '': if r == '':
return httpwrappers.HttpResponseGone() return httpwrappers.HttpResponseGone()
return httpwrappers.HttpResponseRedirect(r.new_path) return httpwrappers.HttpResponsePermanentRedirect(r.new_path)
# No redirect was found. Return the response. # No redirect was found. Return the response.
return response return response

View File

@ -46,7 +46,7 @@ class CommonMiddleware:
newurl = new_url[1] newurl = new_url[1]
if request.GET: if request.GET:
newurl += '?' + request.GET.urlencode() newurl += '?' + request.GET.urlencode()
return httpwrappers.HttpResponseRedirect(newurl) return httpwrappers.HttpResponsePermanentRedirect(newurl)
return None return None

View File

@ -212,6 +212,12 @@ class HttpResponseRedirect(HttpResponse):
self['Location'] = redirect_to self['Location'] = redirect_to
self.status_code = 302 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): class HttpResponseNotModified(HttpResponse):
def __init__(self): def __init__(self):
HttpResponse.__init__(self) HttpResponse.__init__(self)

View File

@ -1,28 +1,28 @@
from django.core.extensions import DjangoContext, render_to_response 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): 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 }}``. ``{{ params }}``.
""" """
return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request)) return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request))
def redirect_to(request, url, **kwargs): def redirect_to(request, url, **kwargs):
""" """
Redirect to a given URL. Redirect to a given URL.
The given url may contain dict-style string formatting which will be The given url may contain dict-style string formatting, which will be
interpolated against the params in the URL. For example, to redirect from 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('', urlpatterns = patterns('',
('^foo/(?p<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}), ('^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 the given url is ``None``, a HttpResponseGone (410) will be issued.
""" """
if url is not None: if url is not None:
return HttpResponseRedirect(url % kwargs) return HttpResponsePermanentRedirect(url % kwargs)
else: else:
return HttpResponseGone() return HttpResponseGone()

View File

@ -357,7 +357,14 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in
``HttpResponseRedirect`` ``HttpResponseRedirect``
The constructor takes a single argument -- the path to redirect to. This 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 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`` ``HttpResponseNotModified``
The constructor doesn't take any arguments. Use this to designate that a The constructor doesn't take any arguments. Use this to designate that a