How to detect the Android foldable/flip phone in Djagno

My project displays templates separately from PC/tablet and mobile.

  • views.py
...
from django_user_agents.utils import get_user_agent
...

class indexView(View):
    def get(self, request, *args, **kwargs):
        ...
        user = get_user_agent(request)
        if user.is_pc or user.is_tablet:
            template_name = 'web/index.html'     # for PC and Tablet
        else :
            template_name = 'mobile/index.html'  # for Mobile
        ...

However, Galaxy z fold 4 is recognized as tablet when folded and opened in the Chrome browser. In Samsung's basic browser, when folded, it is displayed as a mobile template.

When I checked the userAgent, it included "Safari" instead of "Mobile Safari" in the Chrome browser.

Mozilla/5.0 (Linux; Android 13; SM-F936N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

When the foldable phone is folded, I want to display it as a mobile template on the cover screen in the Chrome browser.

Is there a way to detect a foldable phone in Django? Or if you have another good idea, please recommend it :)

As far as I can see only the template changes based on if it is a tablet or pc, you can use a single template which would change based on the aspect ratio of the user.

Back to Top