Kaydet (Commit) 3234a932 authored tarafından Adrian Holovaty's avatar Adrian Holovaty

Fixed #1117 -- Added HttpResponsePermanentRedirect

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1816 bcc190cf-cafb-0310-a4f2-bffc1f526a37
üst 6cca8069
......@@ -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
......@@ -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
......
......@@ -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)
......
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()
\ No newline at end of file
return HttpResponseGone()
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment