AppEngine サイトに Tipfy RequestHandler をサブクラス化する BaseHandler クラスがあります。その中で、デバイス名を含むクラス属性 (タプル) を使用して、モバイル デバイス用の "貧弱な" ブラウザー スニファーをセットアップしました。
後続のメソッドでは、タプル内のデバイス名をループして、Request オブジェクトのユーザー エージェント文字列と照合します。一致した場合は、「is_mobile」というインスタンス属性を True に設定します。
ただし、そのメソッドでは、Python から「TypeError: 型 'UserAgent' の引数は反復可能ではありません」というエラーが表示されますが、その理由はわかりません。ループ。
コードは次のとおりです。
class BaseHandler(RequestHandler, AppEngineAuthMixin, AllSessionMixins):
mobile_devices = ('Android', 'iPhone', 'iPod', 'Blackberry')
....
def detect_mobile_devices(self):
found_device = False
for device in self.__class__.mobile_devices:
if device in self.request.user_agent:
found_device = True
break
self.is_mobile = found_device
Python が気に入らない行は次のとおりです。
File "/path/to/project/app/apps/remember_things/handlers.py", line 56, in detect_mobile_devices
if device in self.request.user_agent: