Determine content-type of page in middleware class Django

44 views
Skip to first unread message

Artie

unread,
Dec 12, 2014, 3:57:49 AM12/12/14
to django...@googlegroups.com

Well, I have middleware class which is required to determine content-type of rendered page and if it is 'txt/html', then do some action. I've started just from seeing what content-types do I have on page and here is first problem I faced:

class StatsMiddleware(object):
   
def process_view(self, request, view_func, view_args, view_kwargs):
      response
= view_func(request, *view_args, **view_kwargs)
     
print response['Content-Type']

Executing this, I get few messages about content-types of page elements, like text/html, application/javascript and also tons of errors like key error: content-type and after that - broken pipe

So I assume that not all elements of page have such header Content-Type and my question is following:

Is there some general Content-Type that says 'That page is text/html' or there are a lot of content-types on page ?

And also if this is a proper way to deterimne content-type of page like this:

if response['Content-Type'] == 'text/html':
     pass

Collin Anderson

unread,
Dec 14, 2014, 2:00:36 PM12/14/14
to django...@googlegroups.com
Hi,

It looks like for 304 (not modified) Django specifically deletes the content-type header.


It would probably be better to say:

if response.get('Content-Type', '').startswith('text/html'):
   
pass

Collin
Reply all
Reply to author
Forward
0 new messages