Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different response classes via klass param #11162

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions django/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
from django.http import (
Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect,
)
from django.http.response import HttpResponseBase
from django.template import loader
from django.urls import NoReverseMatch, reverse
from django.utils.functional import Promise


def render(request, template_name, context=None, content_type=None, status=None, using=None):
def render(request, template_name, context=None, content_type=None,
status=None, using=None, klass=HttpResponse):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response_class instead of klass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I am kind of indifferent, just used klass because I have seen it in other libraries. I'm not seeing any evidence empirically that Django has a preference but maybe it does.

"""
Return a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.

klass may be any subclass of http.response.HttpResponseBase, and
defaults to http.HttpResponse.
"""
if not issubclass(klass, HttpResponseBase):
raise TypeError("`klass` must be HttpResponseBase subclass")
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)
return klass(content, content_type, status)


def redirect(to, *args, permanent=False, **kwargs):
Expand Down