What would be the best way to go about forcing all HttpResponse
objects returned by views in the Django admin site app to use "text/html" as their content type, regardless of the value of the DEFAULT_CONTENT_TYPE
setting? My project has this set to "application/xhtml+xml," and although content produced by the admin app claims to be valid XHTML (look at its doctype declaration), it is not. Ticket #5704 is a major problem, and I discovered some issues with inline forms (namely a liberal use of
, which is not a named entity in XHTML). A comment in Ticket #11684 indicates that it may be a while before the admin site will fully support XHTML, so I need to figure out how to use "text/html" for the admin site, while leaving the default as "application/xhtml+xml."
views:
7answers:
1
A:
I'm not sure if this is the best way to do it or not, but I finally achieved my goal by subclassing AdminSite
and overriding the admin_view
method:
class HTMLAdminSite(admin.AdminSite):
'''Django AdminSite that forces response content-types to be text/html
This class overrides the default Django AdminSite `admin_view` method. It
decorates the view function passed and sets the "Content-Type" header of
the response to 'text/html; charset=utf-8'.
'''
def _force_html(self, view):
def force_html(*arguments, **keywords):
response = view(*arguments, **keywords)
response['Content-Type'] = 'text/html; charset=utf-8'
return response
return force_html
def admin_view(self, view, *arguments, **keywords):
return super(HTMLAdminSite, self).admin_view(self._force_html(view),
*arguments, **keywords)
Then, in my root URLconf, before calling admin.autodiscover()
, I set admin.site
to an instance of this HTMLAdminSite
class. It seems to work okay, but if there's a better way, I'd be glad to hear it.
AdmiralNemo
2010-07-22 04:28:07