Skip to content

Commit

Permalink
Implement the Django 1.3 render() shortcut.
Browse files Browse the repository at this point in the history
Closes #21.
  • Loading branch information
miracle2k committed Feb 7, 2012
1 parent e325ad3 commit aaea5d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 27 additions & 1 deletion coffin/shortcuts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.shortcuts import *


__all__ = ('render_to_string', 'render_to_response',)
__all__ = ('render_to_string', 'render_to_response', 'render')


# Is within ``template.loader`` as per Django specification -
Expand All @@ -23,3 +23,29 @@ def render_to_response(template_name, dictionary=None, context_instance=None,
"""
rendered = render_to_string(template_name, dictionary, context_instance)
return HttpResponse(rendered, mimetype=mimetype)


def render(request, *args, **kwargs):
"""
Returns a HttpResponse whose content is filled with the result of calling
coffin.template.loader.render_to_string() with the passed arguments.
Uses a RequestContext by default.
"""
httpresponse_kwargs = {
'content_type': kwargs.pop('content_type', None),
'status': kwargs.pop('status', None),
}

if 'context_instance' in kwargs:
context_instance = kwargs.pop('context_instance')
if kwargs.get('current_app', None):
raise ValueError('If you provide a context_instance you must '
'set its current_app before calling render()')
else:
current_app = kwargs.pop('current_app', None)
context_instance = RequestContext(request, current_app=current_app)

kwargs['context_instance'] = context_instance

return HttpResponse(render_to_string(*args, **kwargs),
**httpresponse_kwargs)
5 changes: 5 additions & 0 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_render():
"""Test the render shortcut."""
from coffin.shortcuts import render
response = render(None, 'render-x.html', {'x': 'foo'})
assert response.content == 'foo'

0 comments on commit aaea5d2

Please sign in to comment.