Opened 9 years ago

Closed 8 years ago

Last modified 2 years ago

#1504 closed enhancement (fixed)

Allow render_to_response() to take a content-type param

Reported by: django@… Owned by: nobody
Component: Template system Version: master
Severity: normal Keywords: sprintsept14
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently there is no way to pass the Content-Type header field while using render_to_response(). This means dropping down to using template_loader() and HttpResponse objects to handle this fairly trivial task.

A simple change to render_to_response() would allow the developer to pass that information along:

def render_to_response(*args, **kwargs):
    ctype = kwargs.pop('content_type', None)
    return HttpResponse(loader.render_to_string(*args, **kwargs), mimetype=c
type)
load_and_render = render_to_response # For backwards compatibility.

To use it, simply do:

    return render_to_response( 'imageview/gallery', content_dictionary,
        content_type = "application/xhtml+xml; charset=%s" % DEFAULT_CHARSET,
        context_instance = DjangoContext(request) )

Attachments (5)

__init__.py.diff (662 bytes) - added by James Wheare <django@…> 8 years ago.
__init__.py.2.diff (791 bytes) - added by Rob van der Linde 8 years ago.
Add support for mimetype in render_to_response() function
__init__.py.3.diff (743 bytes) - added by Rob van der Linde 8 years ago.
Add support for mimetype in render_to_response() function
templates_python.txt.diff (1.5 KB) - added by anonymous 8 years ago.
1504.diff (2.4 KB) - added by minarets 8 years ago.
diff for code and docs

Download all attachments as: .zip

Change History (22)

comment:1 Changed 9 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

It doesn't mean dropping down to template_loader and HttpResponse objects. You can assign the mimetype to an HttpResponse object after the fact:

r = render_to_response('imageview/gallery', context_dict)
r.mimetype = 'application/xhtml+xml'
return r

comment:2 Changed 9 years ago by emeraldjunk-django@…

For the dev version, revision 2923 (updated May 16) this works for me instead:

r.headersContent-Type? = 'text/xml; charset=utf-8'

Cheers
Stefan

comment:3 Changed 8 years ago by anonymous

I came here via a Google Search and the above helped, but is slightly incorrect.

As stated above you can modify a HttpResponse object before returning it. All HTTPResponse objects have a dictionary called "headers". So to change the HTTPResponse into XML.

r = render_to_response('imageview/gallery', context_dict)
r.headers['Content-Type'] = 'text/xml; charset=utf-8'
return r

comment:4 Changed 8 years ago by James Wheare <django@…>

  • Component changed from Tools to Template system
  • Has patch set
  • Resolution wontfix deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Design decision needed
  • Version set to SVN

Hmm, this seems less elegant now, and kind of a shame to have to add an extra line of code when it really makes sense as a parameter. Here's a patch, could this be re-reviewed?

Changed 8 years ago by James Wheare <django@…>

comment:5 Changed 8 years ago by depth.of.field@…

Came here through google search looking for a way to do just this... I think it should be an optional named parameter. It's a fairly common task, and you need to be quite knowledgable of how Django works to do it at the moment.

comment:6 Changed 8 years ago by anonymous

Another vote for having an optional named parameter for this.

comment:7 Changed 8 years ago by John Shaffer <jshaffer2112@…>

If accepted, the nameed paramater should probably be called content_type rather than mimetype. See #3526.

comment:8 Changed 8 years ago by Simon G. <dev@…>

Can someone make a case for this on Django-developers?

comment:9 Changed 8 years ago by Rob van der Linde

I tried the first attached diff by James Wheare, but it didn't work for me, his method started like:

def render_to_response(mimetype=None, *args, **kwargs):

However, trying to call that method as follows:

def index(request):
	return render_to_response("myapp/index.html", {"foo": "bar"}, mimetype="application/xhtml+xml")

Results in the following error:

non keyword arg after keyword arg

I tried both adjusting the way I call his method, and the order of his methods parameters itself, in hope I could get it working, but no luck, I kept getting the same error.

So I fixed the bug myself and created a .diff file. I have throughly tested this and it works for all these cases:

def index(request):
	return render_to_response("myapp/index.html", {"foo": "bar"}, mimetype="application/xhtml+xml")
def index(request):
	return render_to_response("myapp/index.html", {"foo": "bar"})
def index(request):
	return render_to_response("myapp/index.html", mimetype="application/xhtml+xml")
def index(request):
	return render_to_response("myapp/index.html")

I used mimetype as the parameter name, since that was the same naming convention as in HttpResponse, but if anyone wants to change it to content_type instead, that's fine.

I hope this can be included soon, and this longstanding feature resolved. Many people would benefit from this, not only is it useful for serving XHTML files, but also RSS, XUL, and other XML based formats.

I will attach the .diff file.

Changed 8 years ago by Rob van der Linde

Add support for mimetype in render_to_response() function

Changed 8 years ago by Rob van der Linde

Add support for mimetype in render_to_response() function

comment:10 Changed 8 years ago by ubernostrum

  • Needs documentation set
  • Triage Stage changed from Design decision needed to Accepted

Changed 8 years ago by anonymous

comment:11 Changed 8 years ago by minarets

  • Needs documentation unset

Added documentation.

comment:12 Changed 8 years ago by ubernostrum

  • Keywords sprintsep14 added
  • Needs documentation set
  • Patch needs improvement set

Changed 8 years ago by minarets

diff for code and docs

comment:13 Changed 8 years ago by ubernostrum

  • Needs documentation unset
  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:14 Changed 8 years ago by ubernostrum

  • Keywords sprintsept14 added; sprintsep14 removed

comment:15 Changed 8 years ago by adrian

I'm working on reviewing this and checking it in.

comment:16 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [6217]) Fixed #1504 -- render_to_response() now takes a mimetype parameter

comment:17 Changed 2 years ago by mk.faraz@…

  • Easy pickings unset
  • UI/UX unset

I used following code for this, just place it in views.py after import, offcourse you can replace direct_to_template to render_to_response in similar manner

direct_to_template = direct_to_template
def
view_direct_to_template(*args, kwargs):

content_type = kwargs.pop('content_type', None)
r = direct_to_template(*args, kwargs)
if content_type:

rContent-Type? = content_type

return r

direct_to_template = view_direct_to_template

Note: See TracTickets for help on using tickets.
Back to Top