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