Opened 9 years ago

Closed 9 years ago

Last modified 9 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: UI/UX:

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 Changed 9 years ago by anonymous

  • Component changed from Admin interface to Generic views
  • Owner changed from adrian to jacob

comment:2 Changed 9 years ago by anonymous

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

comment:3 Changed 9 years ago by EspenG

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

comment:4 Changed 9 years ago by ian@…

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

comment:5 Changed 9 years ago by EspenG

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 Changed 9 years ago by adrian

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

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

comment:7 Changed 9 years ago by URL

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