Skip to content
Browse files

Add support for status and content_type in Resource.render

Remove argument unpacking of view args and kwargs.
This is to prevent conflict of other argument names in the method definition.
  • Loading branch information...
1 parent c1eb978 commit b7f82699ab7792d96dc73ac4afb350fc05703ae5 @bruth committed May 3, 2013
Showing with 14 additions and 6 deletions.
  1. +8 −2 restlib2/
  2. +6 −4 restlib2/
10 restlib2/
@@ -1,19 +1,25 @@
from django.template import loader
from django.template import RequestContext
from django.http import HttpResponse
+from .http import codes
class TemplateResponseMixin(object):
template_name = None
template_string = None
- def render(self, request, context, *args, **kwargs):
+ def render(self, request, context, status=codes.ok, content_type=None,
+ args=None, kwargs=None):
"Expects the method handler to return the `context` for the template."
if isinstance(self.template_name, (list, tuple)):
template = loader.select_template(self.template_name)
elif self.template_name:
template = loader.get_template(self.template_name)
template = loader.Template(self.template_string)
context = RequestContext(request, context)
- return HttpResponse(template.render(context))
+ content = template.render(context)
+ return HttpResponse(content, status=status, content_type=content_type)
10 restlib2/
@@ -264,20 +264,22 @@ def dispatch(self, request, *args, **kwargs):
# If the return value of the handler is not a response, pass
# the return value into the render method.
if not isinstance(response, HttpResponse):
- response = self.render(request, response, *args, **kwargs)
+ response = self.render(request, response, args=args, kwargs=kwargs)
# Process the response, check if the response is overridden and
# use that instead.
return self.process_response(request, response)
- def render(self, request, content, *args, **kwargs):
+ def render(self, request, content, status=codes.ok, content_type=None,
+ args=None, kwargs=None):
"Renders the response based on the content returned from the handler."
- response = HttpResponse()
+ response = HttpResponse(status=status, content_type=content_type)
if request.method != methods.HEAD:
if isinstance(content, (basestring, file)):
response.content = content
- else:
+ elif not content_type:
accept_type = getattr(request, '_accept_type', None)
if accept_type and serializers.supports_encoding(accept_type):
response.content = serializers.encode(accept_type, content)

0 comments on commit b7f8269

Please sign in to comment.
Something went wrong with that request. Please try again.