代码之家  ›  专栏  ›  技术社区  ›  AdmiralNemo

忽略Django管理站点的默认内容类型?

  •  0
  • AdmiralNemo  · 技术社区  · 16 年前

    什么是最好的办法去强迫所有人 HttpResponse DEFAULT_CONTENT_TYPE 设置?我的项目将此设置为“application/xhtml+xml”,尽管管理应用程序生成的内容声称是有效的xhtml(查看其doctype声明),但事实并非如此。 Ticket #5704 是一个主要问题,我发现了内联表单的一些问题(即   ,它不是XHTML中的命名实体)。评论 Ticket #11684 表示在管理站点完全支持XHTML之前可能需要一段时间,因此我需要弄清楚如何在管理站点中使用“text/html”,而将默认值保留为“application/XHTML+xml”

    1 回复  |  直到 16 年前
        1
  •  1
  •   AdmiralNemo    16 年前

    我不确定这是不是最好的方法,但我最终通过子类化实现了我的目标 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

    推荐文章