App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / django / views / generic / simple.py
blobf63860a4cf2613ab75cdeab37d1034244fbf4620
1 from django.template import loader, RequestContext
2 from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone
3 from django.utils.log import getLogger
5 import warnings
6 warnings.warn(
7 'Function-based generic views have been deprecated; use class-based views instead.',
8 DeprecationWarning
11 logger = getLogger('django.request')
14 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
15 """
16 Render a given template with any extra URL parameters in the context as
17 ``{{ params }}``.
18 """
19 if extra_context is None: extra_context = {}
20 dictionary = {'params': kwargs}
21 for key, value in extra_context.items():
22 if callable(value):
23 dictionary[key] = value()
24 else:
25 dictionary[key] = value
26 c = RequestContext(request, dictionary)
27 t = loader.get_template(template)
28 return HttpResponse(t.render(c), content_type=mimetype)
30 def redirect_to(request, url, permanent=True, query_string=False, **kwargs):
31 """
32 Redirect to a given URL.
34 The given url may contain dict-style string formatting, which will be
35 interpolated against the params in the URL. For example, to redirect from
36 ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf::
38 urlpatterns = patterns('',
39 ('^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
42 If the given url is ``None``, a HttpResponseGone (410) will be issued.
44 If the ``permanent`` argument is False, then the response will have a 302
45 HTTP status code. Otherwise, the status code will be 301.
47 If the ``query_string`` argument is True, then the GET query string
48 from the request is appended to the URL.
50 """
51 args = request.META.get('QUERY_STRING', '')
53 if url is not None:
54 if kwargs:
55 url = url % kwargs
57 if args and query_string:
58 url = "%s?%s" % (url, args)
60 klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect
61 return klass(url)
62 else:
63 logger.warning('Gone: %s', request.path,
64 extra={
65 'status_code': 410,
66 'request': request
68 return HttpResponseGone()