1 from django
.template
import loader
, RequestContext
2 from django
.http
import HttpResponse
, HttpResponseRedirect
, HttpResponsePermanentRedirect
, HttpResponseGone
3 from django
.utils
.log
import getLogger
7 'Function-based generic views have been deprecated; use class-based views instead.',
11 logger
= getLogger('django.request')
14 def direct_to_template(request
, template
, extra_context
=None, mimetype
=None, **kwargs
):
16 Render a given template with any extra URL parameters in the context as
19 if extra_context
is None: extra_context
= {}
20 dictionary
= {'params': kwargs
}
21 for key
, value
in extra_context
.items():
23 dictionary
[key
] = value()
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
):
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.
51 args
= request
.META
.get('QUERY_STRING', '')
57 if args
and query_string
:
58 url
= "%s?%s" % (url
, args
)
60 klass
= permanent
and HttpResponsePermanentRedirect
or HttpResponseRedirect
63 logger
.warning('Gone: %s', request
.path
,
68 return HttpResponseGone()