django库为不同的设备类型提供不同的模板
django-devicetype-templates的Python项目详细描述
django设备类型模板是一个库,它通过浏览器的用户代理字符串检测设备类型 为每种类型提供不同的模板。
如果标准的响应式布局不能满足您的所有需求 如果你不想使用一些带有线程局部变量的黑客模板加载程序, 你会发现这个图书馆很有用。
要求
- 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 ^{tt1}$.
安装
从pypi安装:
pip install django-devicetype-templates
将开发版本安装到virtualenv:
git clone https://github.com/whit/django-devicetype-templates.git cd django-devicetype-templates python setup.py develop
运行测试:
pip intsall -r requirements-dev.txt py.test
配置
添加中间件:
MIDDLEWARE_CLASSES = ( ... 'devicetype.middleware.DeviceTypeMiddleware', )
如果需要在模板中使用某些变量,可以添加设备类型上下文处理器:
TEMPLATE_CONTEXT_PROCESSORS = ( ... 'devicetype.context_processors.devicetype', )
然后,在模板中将提供以下变量:devicetype,is_mobile,is_tablet 和big_resolution(尚未实现)。
其他设置
DEVICETYPE_TEMPLATE_PREFIX
前缀是可变的。当需要前缀模板文件名时,请使用tablet-之类的内容。如果你想拥有 在子文件夹中的设备特定模板中,可以使用tablet/前缀。
默认值:
{ 'desktop': '', 'mobile': 'mobile/', 'tablet': 'tablet/', }
DEVICETYPE_PREFIX_BASENAME
当您使用类似文件夹的前缀时,如tablet/和mobile/,使用此设置 您可以选择如何检测子文件夹。
使用DEVICETYPE_PREFIX_BASENAME = False(默认值):
tablet/base.html tablet/app/app_base.html tablet/layout/three-cols.html ...
使用DEVICETYPE_PREFIX_BASENAME = True:
tablet/base.html app/tablet/app_base.html layout/tablet/three-cols.html ...
DEVICETYPE_MOBILE_PATTERNS和DEVICETYPE_TABLET_PATTERNS
可以覆盖默认搜索模式。它首先搜索平板电脑的模式。