Opened 19 years ago

Closed 19 years ago

Last modified 19 years ago

#2000 closed (fixed)

[patch] allow generic views to respond with non text/html content types

Reported by: ian@… Owned by: Jacob
Component: Generic views Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I needed to have a page respond with a text/plain content type.
here's the patch

Index: views/generic/list_detail.py
===================================================================
--- views/generic/list_detail.py        (revision 2970)
+++ views/generic/list_detail.py        (working copy)
@@ -6,7 +6,8 @@
 
 def object_list(request, queryset, paginate_by=None, allow_empty=False,
         template_name=None, template_loader=loader,
-        extra_context={}, context_processors=None, template_object_name='object'):
+        extra_context={}, context_processors=None, template_object_name='object',
+        mimetype=None):
     """
     Generic list of objects.
 
@@ -73,12 +74,13 @@
         model = queryset.model
         template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     t = template_loader.get_template(template_name)
-    return HttpResponse(t.render(c))
+    return HttpResponse(t.render(c), mimetype=mimetype)
 
 def object_detail(request, queryset, object_id=None, slug=None,
         slug_field=None, template_name=None, template_name_field=None,
         template_loader=loader, extra_context={},
-        context_processors=None, template_object_name='object'):
+        context_processors=None, template_object_name='object',
+        mimetype=None):
     """
     Generic list of objects.
 
@@ -113,6 +115,6 @@
             c[key] = value()
         else:
             c[key] = value
-    response = HttpResponse(t.render(c))
+    response = HttpResponse(t.render(c), mimetype=mimetype)
     populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name))
     return response

Change History (7)

comment:1 by anonymous, 19 years ago

Component: Admin interfaceGeneric views
Owner: changed from Adrian Holovaty to Jacob

comment:2 by anonymous, 19 years ago

Summary: allow generic views to respond with non text/html content types[patch] allow generic views to respond with non text/html content types

comment:3 by EspenG, 19 years ago

Should not a default mimetype be set in stead of None?

comment:4 by ian@…, 19 years ago

Hi Espen,
passing 'None' to HttpResponse *is* the default, and lets HttpResponse figure out the correct default.

comment:5 by EspenG, 19 years ago

Sorry about that then. I taugth that when the default was 'None' I had to pass along a mimetype every time, but as you say that was wrong then.

comment:6 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

(In [3022]) Fixed #2000 -- Added 'mimetype' parameter to generic views. Thanks, Ian Holsman

comment:7 by URL, 19 years ago

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