私のテンプレートには既にデバイス検出(http://djangosnippets.org/snippets/2228/)が使用されており、ビューでも動作させようとしているので、ユーザーがiPhoneから来た場合はアプリストアにリダイレクトできます。Django - ビューでモバイルデバイスを検出する
だから私はすでに持っていた:その後、
import re
def mobile(request):
device = {}
ua = request.META.get('HTTP_USER_AGENT', '').lower()
if ua.find("iphone") > 0:
device['iphone'] = "iphone" + re.search("iphone os (\d)", ua).groups(0)[0]
if ua.find("ipad") > 0:
device['ipad'] = "ipad"
if ua.find("android") > 0:
device['android'] = "android" + re.search("android (\d\.\d)", ua).groups(0)[0].translate(None, '.')
# spits out device names for CSS targeting, to be applied to <html> or <body>.
device['classes'] = " ".join(v for (k,v) in device.items())
return {'device': device }
そしてツール/ middleware.pyでクラスを作成しました:
from tools.context_processor import mobile
class detect_device(object):
def process_request(self, request):
device = mobile(request)
request.device = device
はsettings.pyにMIDDLEWARE_CLASSESに次を追加しました:
'tools.middleware.detect_device'
を
Views.pyで作成しました:
def get_link(request):
if request.device.iphone:
app_store_link = settings.APP_STORE_LINK
return HttpResponseRedirect(app_store_link)
else:
return HttpResponseRedirect('/')
しかし、私はエラーを取得しています:
'dict' object has no attribute 'iphone'
、 'それは理論的に0あなたのチェックは、この' ua.find場合(「iphone」)のようになります返すことができますので、最初のマッチのインデックスを返しますfind'> - 代わりに-1を返します。一致しない場合は-1を返します。 –