diff --git a/django/contrib/redirects/middleware.py b/django/contrib/redirects/middleware.py index c8f09ada8e..c005eba9ac 100644 --- a/django/contrib/redirects/middleware.py +++ b/django/contrib/redirects/middleware.py @@ -21,7 +21,7 @@ class RedirectFallbackMiddleware: 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 diff --git a/django/middleware/common.py b/django/middleware/common.py index 7ab2841118..aa8f4ec071 100644 --- a/django/middleware/common.py +++ b/django/middleware/common.py @@ -46,7 +46,7 @@ class CommonMiddleware: newurl = new_url[1] if request.GET: newurl += '?' + request.GET.urlencode() - return httpwrappers.HttpResponseRedirect(newurl) + return httpwrappers.HttpResponsePermanentRedirect(newurl) return None diff --git a/django/utils/httpwrappers.py b/django/utils/httpwrappers.py index 6a64592eaa..6fda8ad570 100644 --- a/django/utils/httpwrappers.py +++ b/django/utils/httpwrappers.py @@ -212,6 +212,12 @@ class HttpResponseRedirect(HttpResponse): 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) diff --git a/django/views/generic/simple.py b/django/views/generic/simple.py index 8a054e1ce7..086ed42805 100644 --- a/django/views/generic/simple.py +++ b/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//`` to ``/bar//``, you could use the following urlpattern:: + ``/foo//`` to ``/bar//``, you could use the following URLconf:: urlpatterns = patterns('', ('^foo/(?p\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() \ No newline at end of file + return HttpResponseGone() diff --git a/docs/request_response.txt b/docs/request_response.txt index dea1cdb975..76882953bb 100644 --- a/docs/request_response.txt +++ b/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