2006-05-02 09:31:56 +08:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
|
2005-11-16 00:55:26 +08:00
|
|
|
|
|
|
|
def direct_to_template(request, template, **kwargs):
|
2005-11-16 01:19:33 +08:00
|
|
|
"""
|
2006-01-04 07:57:14 +08:00
|
|
|
Render a given template with any extra URL parameters in the context as
|
2005-11-16 01:19:33 +08:00
|
|
|
``{{ params }}``.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
return render_to_response(template, {'params' : kwargs}, context_instance=RequestContext(request))
|
2006-01-04 07:57:14 +08:00
|
|
|
|
2005-11-16 01:19:33 +08:00
|
|
|
def redirect_to(request, url, **kwargs):
|
|
|
|
"""
|
2006-01-04 07:57:14 +08:00
|
|
|
Redirect to a given URL.
|
|
|
|
|
|
|
|
The given url may contain dict-style string formatting, which will be
|
2005-11-16 01:19:33 +08:00
|
|
|
interpolated against the params in the URL. For example, to redirect from
|
2006-01-04 07:57:14 +08:00
|
|
|
``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf::
|
2005-11-16 01:19:33 +08:00
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
('^foo/(?p<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
|
|
|
|
)
|
2006-01-04 07:57:14 +08:00
|
|
|
|
2005-11-16 01:19:33 +08:00
|
|
|
If the given url is ``None``, a HttpResponseGone (410) will be issued.
|
|
|
|
"""
|
|
|
|
if url is not None:
|
2006-01-04 07:57:14 +08:00
|
|
|
return HttpResponsePermanentRedirect(url % kwargs)
|
2005-11-16 01:19:33 +08:00
|
|
|
else:
|
2006-01-04 07:57:14 +08:00
|
|
|
return HttpResponseGone()
|