忽略 Django 管理站点的 DEFAULT_CONTENT_TYPE?

发布于 2024-09-10 13:28:44 字数 579 浏览 3 评论 0原文

强制 Django 管理站点应用程序中的视图返回的所有 HttpResponse 对象使用“text/html”作为其内容类型的最佳方法是什么,无论 的值是什么DEFAULT_CONTENT_TYPE 设置?我的项目将此设置为“application/xhtml+xml”,尽管管理应用程序生成的内容声称是有效的 XHTML(查看其 doctype 声明),但事实并非如此。 Ticket #5704 是一个主要问题,我发现内联表单(即自由表单)存在一些问题使用  ,它不是 XHTML 中的命名实体)。 Ticket #11684 中的评论表明管理站点可能需要一段时间才能完全支持XHTML,所以我需要弄清楚如何在管理站点中使用“text/html”,同时保留默认值“application/xhtml+xml”。

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."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2024-09-17 13:28:44

我不确定这是否是最好的方法,但我最终通过子类化 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)

然后,在我的root URLconf,在调用 admin.autodiscover() 之前,我将 admin.site 设置为该 HTMLAdminSite 类的实例。它似乎工作正常,但如果有更好的方法,我很高兴听到。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文