Detect mobile, tablet or Desktop on Django

后端 未结 5 1449
终归单人心
终归单人心 2021-02-01 19:56

I need to detect 3 types of device: tablet, mobile or desktop.

I found a script for detecting mobile on github, but how I can det

5条回答
  •  无人共我
    2021-02-01 20:28

    Use django-user_agents

    from django_user_agents.utils import get_user_agent
    
    def my_view(request):
        user_agent = get_user_agent(request)
        if user_agent.is_mobile:
            # identified as a mobile phone (iPhone, Android phones, Blackberry, Windows Phone devices etc)
        if user_agent.is_tablet:
            # identified as a tablet device (iPad, Kindle Fire, Nexus 7 etc)
        if user_agent.is_pc:
            # identified to be running a traditional "desktop" OS (Windows, OS X, Linux)
    

    It uses user_agents under the hood.

提交回复
热议问题