【问题标题】:How to block internet explorer on my Django web app?如何在我的 Django Web 应用程序上阻止 Internet Explorer?
【发布时间】:2013-08-22 20:17:02
【问题描述】:

我正在开发一个不能很好地与 Internet Explorer 配合使用的网络应用程序(网络套接字、json、安全问题)。

目前,在我的应用程序使用 IE 之前:

如何拒绝来自 Internet Explorer 客户端的连接?

谢谢

【问题讨论】:

  • 请不要这样做。给他们一条好消息,说明您的应用不兼容,并鼓励他们使用新的浏览器或安装 chrome 框架。
  • 我通常同意尼克的观点,但因为它是 IE,just kill their browser
  • <!--[if IE]> <div class="error">This service is currently incompatible with Internet Explorer. Please use another browser until we can correct the issue. We apologize for the inconvenience. <![endif]--> - 一个警告会很好,所以他们至少可以看到它的样子。如果你想直截了当,你可以做类似<!--[if IE]> <!-- <![endif]-->(你的页面内容)<!--[if IE]> --> <![endif]-->这样的事情,如果浏览器是IE,它应该有效地注释掉你的整个代码。不过,我不建议这么直率。
  • @NickTomlin 如果用户仍然尝试我的应用程序,您不认为挫败感会更大吗?我想显示一条消息并拒绝连接。真的很糟糕吗?

标签: django html internet-explorer


【解决方案1】:

创建一个用于解析request.META['HTTP_USER_AGENT'] 的中间件。如果您发现用户使用 IE,请给他一个好消息(例如通知或小警报框),告诉他您的网站没有针对他的浏览器进行优化:)

一些代码示例:Django方式

middleware.py (see the doc for more information)

class RequestMiddleware():
    def process_request(self, request):
        if request.META.has_key('HTTP_USER_AGENT'):
            user_agent = request.META['HTTP_USER_AGENT'].lower()
            if 'trident' in user_agent or 'msie' in user_agent:
                 request.is_IE = True
            else:
                 request.is_IE = False

           # or shortest way:
           request.is_IE = ('trident' in user_agent) or ('msie' in user_agent)

您的基本模板:

{% if request.is_IE %}<div class="alert">Watch out! You're using IE, but unfortunately, this website need HTML5 features...</div>{% endif %}

然后将其添加到您的中间件列表中。

优化:纯HTML方式

如果你只想像我所做的那样显示一条消息,你可以使用 HTML 条件注释:

<!--[if IE]> <div class="alert">...</div><![endif]-->

【讨论】:

  • 不应该是MISE 而不是Trident 吗?
  • @psychok7 确实,MSIE 在用户代理字符串中比 Trident 更频繁:useragentstring.com/pages/Internet%20Explorer 我要修复它
  • According to Microsoft: As of Internet Explorer 10, conditional comments are no longer supported by standards mode. Use feature detection to provide effective fallback strategies for website features that aren't supported by the browser.
【解决方案2】:

还有另一种方法!只需使用设置变量DISALLOWED_USER_AGENTS,并确保在您的站点上安装了CommonMiddleware

例如

import re
DISALLOWED_USER_AGENTS = (re.compile(r'msie\s*[2-7]', re.IGNORECASE), )

干杯!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 2016-04-11
相关资源
最近更新 更多