使用mobileesp库检测请求的移动用户代理。

django-mobileesp的Python项目详细描述


settings.py

MIDDLEWARE_CLASSES = (
    ...
    'django_mobileesp.middleware.UserAgentDetectionMiddleware'
)

from django_mobileesp.detector import mobileesp_agent as agent

DETECT_USER_AGENTS = {
    'is_android': agent.detectAndroid,
    'is_ios': agent.detectIos,
    'is_windows_phone': agent.detectWindowsPhone,
    'is_mobile': agent.detectTierTablet | \
                      agent.detectTierIphone | \
                      agent.detectMobileQuick,
}

如果您更喜欢pythonic方法,可以使用包装代理,如下所示:

from django_mobileesp.detector import python_agent as agent

DETECT_USER_AGENTS = {
    'is_android': agent.detect_android,
    'is_ios': agent.detect_ios,
    'is_windows_phone': agent.detect_windows_phone,
    'is_mobile': agent.detect_tier_tablet | \
                      agent.detect_tier_iphone | \
                      agent.detect_mobile_quick,
}

最后访问视图中定义的属性:

def view(request):
    if request.is_mobile:
        ...

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java:如何使用另一个类中的对象   如何在Java中迭代旁遮普语(阿拉伯语)?   类Java嵌套ArrayList返回对象   java正则表达式以匹配“:”之后包含的字符串   java为什么main()很好地显示“Lukaku”并包含_names()返回null?   java嵌套循环,无法理解如何编写此代码   java使用maven动物嗅探器插件检查自己的API   java上传的文件创建保存以备将来在GWT服务器端使用   java转换为Dalvik格式失败65536限制   Java后端的javascript最佳RIA工具   amazon web服务如何将tar文件从amazonS3 bucket提取到Java中的另一个s3   java如何在hibernate搜索中实现对int值的搜索?   使用Maven初始化引导层JavaFX时发生java错误   java Google登录API例外:10:   java Glassfish 3.1.2加载本机库(.dll)   java在join操作中使用TumblingWindow,但没有将任何元素传输到my JoinFunction   IBatis+Java:检索HashMap   多线程java与scala在单独线程上读取文件