Python

Render different Django templates according to the useragent

Renderizar una plantilla de Django diferente para usuarios de escritorio, tablet o de móvil, detectando el useragent.

django package

Install a django package "django-user-agents"

pip install pyyaml ua-parser user-agents
pip install django-user-agents

settings.py

Configure settings.py

INSTALLED_APPS = (
    ...
    'django_user_agents',
)

views.py

Put conditional on the template

{% if request.user_agent.is_mobile %}
  {% include "mobile.html" %}
{% elseif request.user_agent.is_tablet %}
  {% include "tablet.html" %}
{% else %}
  {% include "desktop.html" %}
{% endif %}