相关疑难解决方法(0)

使用Django和Python 3检测移动设备

我很难找到一种简单的方法来检测请求是否来自我的Django视图中的移动设备.

我试图实现这样的事情:

#views.py

def myfunction(request):

    ...
    if request.mobile:
        is_mobile = True
    else:
        is_mobile = False

    context = {
        ... ,
        'is_mobile': is_mobile,
    }
    return render(request, 'mytemplate.html', context)
Run Code Online (Sandbox Code Playgroud)

并在mytemplate.html:

{% if is_mobile %}    
    show something
{% else %}
    show something else
{% endif %}
Run Code Online (Sandbox Code Playgroud)

我检查过的每个地方(例如这里这里),建议使用minidetector.我已经安装了不同的版本:pip install minidetector,pip install minidetector2以及直接的几个github存储库,但它们都没有与Python 3兼容.

所以这里我的问题:是否有任何与Python 3兼容的minidetector版本/分支?如果没有,有哪些替代方案?

django user-agent mobile-website python-3.x

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

django ×1

mobile-website ×1

python-3.x ×1

user-agent ×1