使用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第三方库


热门话题
在Docker中运行启动脚本的java   java我想知道为什么我的“登录逻辑”总是假条件?   java在SwingWorker线程中执行的“长时间运行的任务”应该是什么?   java MIDI曲目不改变音高或乐器   java Maven替换?   我迫切需要java中的文件输入/输出帮助   java将筛选器集传递到数据库类以供SQL使用的最佳设计   链表为什么我们可以使用Node head=null而不用在Java中实例化“head”?   java如何使用ApachePOI刷新数据透视表并从中读取数据?   java Tapestry日期字段扩展日期范围   java更新gwt CellList中的映像   java是Android中存储字符串和图像数据的最佳方式?   java需要为DAO提供单独的接口和impl   是否可以将自定义Java库导入Gradle buildSrc Java类?   javascript动态添加按钮,同时动态添加文本框