|
1 from django.shortcuts import render_to_response |
|
2 from django.template import loader, RequestContext |
|
3 from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone |
|
4 |
|
5 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs): |
|
6 """ |
|
7 Render a given template with any extra URL parameters in the context as |
|
8 ``{{ params }}``. |
|
9 """ |
|
10 if extra_context is None: extra_context = {} |
|
11 dictionary = {'params': kwargs} |
|
12 for key, value in extra_context.items(): |
|
13 if callable(value): |
|
14 dictionary[key] = value() |
|
15 else: |
|
16 dictionary[key] = value |
|
17 c = RequestContext(request, dictionary) |
|
18 t = loader.get_template(template) |
|
19 return HttpResponse(t.render(c), mimetype=mimetype) |
|
20 |
|
21 def redirect_to(request, url, **kwargs): |
|
22 """ |
|
23 Redirect to a given URL. |
|
24 |
|
25 The given url may contain dict-style string formatting, which will be |
|
26 interpolated against the params in the URL. For example, to redirect from |
|
27 ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf:: |
|
28 |
|
29 urlpatterns = patterns('', |
|
30 ('^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}), |
|
31 ) |
|
32 |
|
33 If the given url is ``None``, a HttpResponseGone (410) will be issued. |
|
34 """ |
|
35 if url is not None: |
|
36 return HttpResponsePermanentRedirect(url % kwargs) |
|
37 else: |
|
38 return HttpResponseGone() |