#1504 closed enhancement (fixed)
Allow render_to_response() to take a content-type param
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Template system | Version: | dev |
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)
Change History (22)
comment:1 by , 19 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 19 years ago
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 by , 18 years ago
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 by , 18 years ago
Component: | Tools → Template system |
---|---|
Has patch: | set |
Resolution: | wontfix |
Status: | closed → reopened |
Triage Stage: | Unreviewed → Design decision needed |
Version: | → 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?
by , 18 years ago
Attachment: | __init__.py.diff added |
---|
comment:5 by , 18 years ago
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:7 by , 17 years ago
If accepted, the nameed paramater should probably be called content_type rather than mimetype. See #3526.
comment:9 by , 17 years ago
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.
by , 17 years ago
Attachment: | __init__.py.2.diff added |
---|
Add support for mimetype in render_to_response() function
by , 17 years ago
Attachment: | __init__.py.3.diff added |
---|
Add support for mimetype in render_to_response() function
comment:10 by , 17 years ago
Needs documentation: | set |
---|---|
Triage Stage: | Design decision needed → Accepted |
by , 17 years ago
Attachment: | templates_python.txt.diff added |
---|
comment:12 by , 17 years ago
Keywords: | sprintsep14 added |
---|---|
Needs documentation: | set |
Patch needs improvement: | set |
comment:13 by , 17 years ago
Needs documentation: | unset |
---|---|
Patch needs improvement: | unset |
Triage Stage: | Accepted → Ready for checkin |
comment:14 by , 17 years ago
Keywords: | sprintsept14 added; sprintsep14 removed |
---|
comment:16 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:17 by , 12 years ago
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
It doesn't mean dropping down to template_loader and HttpResponse objects. You can assign the mimetype to an HttpResponse object after the fact: