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
|
|
|
|
2006-10-30 22:30:43 +08:00
|
|
|
def direct_to_template(request, template, extra_context={}, **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-10-30 22:30:43 +08:00
|
|
|
dictionary = {'params': kwargs}
|
|
|
|
for key, value in extra_context.items():
|
|
|
|
if callable(value):
|
|
|
|
dictionary[key] = value()
|
|
|
|
else:
|
|
|
|
dictionary[key] = value
|
|
|
|
return render_to_response(template, dictionary, 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('',
|
2006-10-30 21:49:45 +08:00
|
|
|
('^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
|
2005-11-16 01:19:33 +08:00
|
|
|
)
|
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()
|