我不确定这是不是最好的方法,但我最终通过子类化实现了我的目标
AdminSite
并覆盖
admin_view
方法:
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)
然后,在我的根URLconf中,在调用
admin.autodiscover()
,我设置
admin.site
一个这样的例子
HTMLAdminSite