为手机/智能手机网站开发的Django框架
项目描述
django-mfw 是一个用于使用Django开发移动站的框架。以下功能可用:
通过HTTP_USER_AGENT检测访问设备并确定设备是否可以处理cookie。
为无法处理cookie的设备提供基于非cookie的会话和CSRF保护。
根据设备信息动态更改加载的模板。这意味着您可以为每种设备(风味模板系统)准备HTML模板。
通过检测到的设备编码对请求和响应进行编码。这是必要的,因为某些日本手机无法处理UTF-8。
在请求和响应中翻译日本表情符号到适当的字符代码或PC图像。
安装
sudo pip install django-mfw
或
sudo pip install git+git://github.com/lambdalisue/django-mfw.git#egg=django-mfw
必需(自动安装)
e4u(emoji4unicode捆绑框架)
快速教程
将 mfw 和 mfw.contrib.emoji 添加到 settings.py 文件中的 INSTALL_APPS 设置中
将 mfw.middleware.device.RequestDeviceDetectionMiddleware 添加到 settings.py 文件中的 MIDDLEWARE_CLASSES 首项,并将 mfw.middleware.device.ResponseDeviceDetectionMiddleware 添加到最后一项
将 mfw.middleware.session.SessionMiddleware 和 mfw.middleware.csrf.CsrfViewMiddleware 添加到 MIDDLEWARE_CLASSES 设置中,并注释掉现有的中间件。
将 mfw.contrib.emoji.middleware.DeviceEmojiTranslationMiddleware 和 mfw.middleware.flavour.DeviceFlavourDetectionMiddleware 添加到 MIDDLEWARE_CLASSES 设置中。
将 mfw.template.loaders.flavour.Loader 添加到 TEMPLATE_LOADERS 首项设置。
将 mfw.context_processors.device 和 mfw.context_processors.flavour 添加到 TEMPLATE_CONTEXT_PROCESSORS 设置中。
下面的代码描述了示例设置。有关详细信息,请参阅 settings.py。
# List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( 'mfw.template.loaders.flavour.Loader', 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', # 'django.template.loaders.eggs.Loader', ) MIDDLEWARE_CLASSES = ( 'mfw.middleware.device.RequestDeviceDetectionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.contrib.sessions.middleware.SessionMiddleware', 'mfw.middleware.session.SessionMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', 'mfw.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'mfw.contrib.emoji.middleware.DeviceEmojiTranslationMiddleware', 'mfw.middleware.flavour.DeviceFlavourDetectionMiddleware', 'mfw.middleware.device.ResponseDeviceDetectionMiddleware', ) TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.request", "mfw.context_processors.device", "mfw.context_processors.flavour", )
用法
设备检测
使用 mfw.middleware.device.DeviceDetectionMiddleware 检测请求设备,并将设备实例存储在 request.device 中,该设备实例具有以下属性
- device.support_cookie
表示设备是否支持 cookie。
- device.kind
该设备的类型。它用于风味模板系统。
- device.name
该设备的名称。它用于风味模板系统。
- device.model
该设备的型号名称。它用于风味模板系统。
- device.version
该设备的版本名称。它用于风味模板系统。
- device.encoding
为设备推荐的一种编码。它用于编码请求/响应
- device.carrier (附加)
移动电话设备的一个属性。设备的运营商名称。
- device.uid (附加)
移动电话设备的一个属性。从运营商服务器传递的用户 ID。
- device.reliable (附加)
移动电话设备的一个属性。如果为 False,则 HTTP_USER_AGENT 可能已被修改,因此传递的用户 ID 不可靠。
Unicode 表情符号和日本运营商表情符号转换
mfw.contrib.emoji.middleware.DeviceEmojiTranslationMiddleware 负责此操作。它检测设备并将 Unicode 表情符号自动转换为运营商编码的表情符号。这就是为什么你不需要关心表情符号的代码。只需以 Unicode 表情符号的形式编写表情符号,然后中间件会正确地转换一切并将响应编码为运营商的编码。
入站翻译也由中间件处理。如果 request.GET 或 request.POST 包含表情符号,中间件会自动将表情符号转换为Unicode表情符号,并将值解码为Unicode。
Unicode表情符号可以在 http://www.unicode.org/~scherer/emoji4unicode/snapshot/full.html 找到。这是 emoji4unicode 项目的一部分,翻译方法使用其转换表。更多信息请参阅 e4u。
风味模板系统
mfw.middleware.flavour.DeviceFlavourDetectionMiddleware 检测设备并自动为设备创建 flavour。这个flavour用于template_name的前缀。所以如果flavour是 smartphone/iphone/1.3 并且调用模板名称是 blogs/post_detail.html,那么 mfw.template.loaders.flavour.Loader 将尝试加载以下文件,除了自己之外,在 TEMPLATE_LOADERS 中列出的模板加载器。
TEMPLATE_DIRECTORY/smartphone/iphone/1.3/blogs/post_detail.html
TEMPLATE_DIRECTORY/smartphone/iphone/blogs/post_detail.html
TEMPLATE_DIRECTORY/smartphone/blogs/post_detail.html
TEMPLATE_DIRECTORY/blogs/post_detail.html
mfw.template.loaders.flavour.Loader 是一个捆绑加载器,加载方法依赖于 TEMPLATE_LOADERS 中列出的模板加载器,所以请确保在 TEMPLATE_LOADERS 中列出了正确的模板加载器。
特别感谢
django-mfw 的概念受到了 django-bpmobile django-mobile 和 emoji4unicode 的启发。
项目详情
django-mfw-0.4.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e725ae904dc2aa11f71c4fdd5022a8e03a8b2ed64f1a5847476451d95abc1824 |
|
MD5 | f3ef931498dfea2057b7de57cf9f6c8c |
|
BLAKE2b-256 | 04f7c785d883eb9db8f4c57bb5730d1ae95020a7f8f8ed41f338a97c81dd3a3e |