Skip to main content

Django library to serve different templates for different device types

Project description

django-devicetype-templates is a library that detect device type by browser’s user agent string and serves different templates for each type.

If standard responsive layouts does not fit all of your needs and if you do not want to use some hacky template loaders with thread locals, you may find this library useful.

Requirements

  • Django >= 1.7

  • Tested with Python versions 2.7, 3.3, 3.4, 3.5

  • It uses process_template_response middleware method, so your views should returns TemplateResponse.

Installation

Install from PyPi:

pip install django-devicetype-templates

Install development version to virtualenv:

git clone https://github.com/whit/django-devicetype-templates.git
cd django-devicetype-templates
python setup.py develop

Run tests:

pip intsall -r requirements-dev.txt
py.test

Configuration

Add middleware:

MIDDLEWARE_CLASSES = (
    ...
    'devicetype.middleware.DeviceTypeMiddleware',
)

If you need to use some variables in your templates, you can add devicetype context processor:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'devicetype.context_processors.devicetype',
)

Then, in templates will be available these variables: devicetype, is_mobile, is_tablet and big_resolution (not implemented yet).

Other settings

DEBUG_DEVICETYPE_NOCOOKIE

For development purposes if you want to switch user agents oftenly. Cookie max age wil be set to 5 seconds.

DEVICETYPE_TEMPLATE_PREFIX

Prefixes are variable. When you need prefix template file name, use something like tablet-. If you want to have device-specific templates in subfolders, you can use tablet/ prefix for example.

Default:

{
    'desktop': '',
    'mobile': 'mobile/',
    'tablet': 'tablet/',
}

DEVICETYPE_PREFIX_BASENAME

When you use folder-like prefix, like tablet/ and mobile/, with this setting you can select how subfolders will be detected.

With DEVICETYPE_PREFIX_BASENAME = False (default):

tablet/base.html
tablet/app/app_base.html
tablet/layout/three-cols.html
...

With DEVICETYPE_PREFIX_BASENAME = True:

tablet/base.html
app/tablet/app_base.html
layout/tablet/three-cols.html
...

DEVICETYPE_MOBILE_PATTERNS and DEVICETYPE_TABLET_PATTERNS

You can override default search patterns. It search in tablet patterns first.

Build status

Master branch:
Travis CI - Distributed build platform for the open source community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-devicetype-templates-0.2.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

django_devicetype_templates-0.2.2-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-devicetype-templates-0.2.2.tar.gz.

File metadata

File hashes

Hashes for django-devicetype-templates-0.2.2.tar.gz
Algorithm Hash digest
SHA256 c425acdd68d4304dcee0a80b303ddadb136da1bce18172f6c6861946e35003c6
MD5 69bd15580634a0aaeb33b2f29b39deb6
BLAKE2b-256 eab56b5148dad5f2c79c0eb07a25130b9717b47b62880ce464b2968f81d6a272

See more details on using hashes here.

File details

Details for the file django_devicetype_templates-0.2.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_devicetype_templates-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b35a469c6e7e1457bf5a55bb41086951196ee5c3f502c3c5ac26acd128a6c26f
MD5 ff97f53a41b0653faddb03cd3619da57
BLAKE2b-256 d66039d7b6fe16744f5cb6608a8cdf3c19d40337bdb52e244bc607b869e27c59

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page