Django

Code

Ticket #1504 (closed: fixed)

Opened 3 years ago

Last modified 1 year ago

Allow render_to_response() to take a content-type param

Reported by: django@55minutes.com Assigned to: nobody
Milestone: Component: Template system
Version: SVN Keywords: sprintsept14
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

__init__.py.diff (0.6 kB) - added by James Wheare <django@sparemint.com> on 04/09/07 18:43:53.
__init__.py.2.diff (0.8 kB) - added by Rob van der Linde on 09/13/07 06:47:58.
Add support for mimetype in render_to_response() function
__init__.py.3.diff (0.7 kB) - added by Rob van der Linde on 09/13/07 07:00:52.
Add support for mimetype in render_to_response() function
templates_python.txt.diff (1.5 kB) - added by anonymous on 09/14/07 14:29:37.
1504.diff (2.4 kB) - added by minarets on 09/14/07 14:36:52.
diff for code and docs

Change History

05/09/06 11:02:26 changed by adrian

  • status changed from new to closed.
  • resolution set to wontfix.

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

05/16/06 15:50:58 changed by emeraldjunk-django@yahoo.com

For the dev version, revision 2923 (updated May 16) this works for me instead: r.headersContent-Type? = 'text/xml; charset=utf-8'

Cheers Stefan

02/15/07 09:06:08 changed 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

04/09/07 18:43:29 changed by James Wheare <django@sparemint.com>

  • status changed from closed to reopened.
  • component changed from Tools to Template system.
  • version set to SVN.
  • has_patch set to 1.
  • resolution deleted.
  • stage changed from Unreviewed to Design decision needed.

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?

04/09/07 18:43:53 changed by James Wheare <django@sparemint.com>

  • attachment __init__.py.diff added.

04/12/07 16:55:24 changed by depth.of.field@gmail.com

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.

07/19/07 12:38:03 changed by anonymous

Another vote for having an optional named parameter for this.

07/19/07 13:04:55 changed by John Shaffer <jshaffer2112@gmail.com>

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

07/19/07 16:34:12 changed by Simon G. <dev@simon.net.nz>

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

09/13/07 06:46:48 changed 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.

09/13/07 06:47:58 changed by Rob van der Linde

  • attachment __init__.py.2.diff added.

Add support for mimetype in render_to_response() function

09/13/07 07:00:52 changed by Rob van der Linde

  • attachment __init__.py.3.diff added.

Add support for mimetype in render_to_response() function

09/14/07 11:22:13 changed by ubernostrum

  • needs_docs set to 1.
  • stage changed from Design decision needed to Accepted.

09/14/07 14:29:37 changed by anonymous

  • attachment templates_python.txt.diff added.

09/14/07 14:30:15 changed by minarets

  • needs_docs deleted.

Added documentation.

09/14/07 14:36:18 changed by ubernostrum

  • keywords set to sprintsep14.
  • needs_better_patch set to 1.
  • needs_docs set to 1.

09/14/07 14:36:52 changed by minarets

  • attachment 1504.diff added.

diff for code and docs

09/14/07 14:43:38 changed by ubernostrum

  • needs_better_patch deleted.
  • stage changed from Accepted to Ready for checkin.
  • needs_docs deleted.

09/14/07 14:51:21 changed by ubernostrum

  • keywords changed from sprintsep14 to sprintsept14.

09/14/07 15:49:09 changed by adrian

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

09/14/07 16:30:13 changed by adrian

  • status changed from reopened to closed.
  • resolution set to fixed.

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


Add/Change #1504 (Allow render_to_response() to take a content-type param)




Change Properties
Action