使用mobileesp库检测请求的移动用户代理。
django-mobileesp的Python项目详细描述
settings.py
MIDDLEWARE_CLASSES = ( ... 'django_mobileesp.middleware.UserAgentDetectionMiddleware' ) from django_mobileesp.detector import mobileesp_agent as agent DETECT_USER_AGENTS = { 'is_android': agent.detectAndroid, 'is_ios': agent.detectIos, 'is_windows_phone': agent.detectWindowsPhone, 'is_mobile': agent.detectTierTablet | \ agent.detectTierIphone | \ agent.detectMobileQuick, }
如果您更喜欢pythonic方法,可以使用包装代理,如下所示:
from django_mobileesp.detector import python_agent as agent DETECT_USER_AGENTS = { 'is_android': agent.detect_android, 'is_ios': agent.detect_ios, 'is_windows_phone': agent.detect_windows_phone, 'is_mobile': agent.detect_tier_tablet | \ agent.detect_tier_iphone | \ agent.detect_mobile_quick, }
最后访问视图中定义的属性:
def view(request): if request.is_mobile: ...