I have a piece of code that sets a request attribute when the user is visiting via my android
subdomain (shown below as part of home.devices.py
. This is then picked up in base.html
and used to include my android-navbar.html
. This works fine on all of my pages except for logout.html
. This is the case whether or not the user is logged in.
As you can see in the template snippet from base.html
below I'm outputting the values for UA and subdomain to the page and this works correctly for all other pages, but returns nothing on the logout page.
I've also checked by adding an assert False
statement which fires if my request.subdomain
attribute isn't set to android
. It isn't being triggered though, so clearly the middleware is working but the subdomain and UA variables aren't being accessed from my logout page.
Why is this happening and how can I fix it?
Possibly relevant bits of code:
app.views
def logout(request):
auth.logout(request)
if not request.subdomain == 'android':
assert False
return render_to_response('registration/logout.html')
app.settings
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'home.device.MobileMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
base.html template snippet
...
{% if request.subdomain == 'android' %}
{% include 'home/android-navbar.html' %}
{% else %}
{% include 'home/navbar.html' %}
{% endif %}
{% endblock %}
</div>
<div>UA is: {{ request.ua_full }}</div>
<div>Subdomain is: {{ request.subdomain }}</div>
....
home.devices.py
# list of mobile User Agents
mobile_uas = [
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-'
]
mobile_ua_hints = [ 'SymbianOS', 'Opera Mini', 'iPhone', 'Mobile Safari' ]
def is_mobile(request):
''' Super simple device detection, returns True for mobile devices '''
mobile_browser = False
ua = request.META['HTTP_USER_AGENT'].lower()[0:4]
ua_full = request.META['HTTP_USER_AGENT']
if (ua in mobile_uas):
mobile_browser = True
else:
for hint in mobile_ua_hints:
if request.META['HTTP_USER_AGENT'].find(hint) > 0:
mobile_browser = True
return mobile_browser
def get_ua(request):
''' Return the name of the user agent '''
ua_full = request.META['HTTP_USER_AGENT']
return ua_full
def get_subdomain(request):
subdomain = request.META['HTTP_HOST'].split('.')[0]
return subdomain
class MobileMiddleware(object):
def __init__(self):
pass
def process_request(self, request):
request.mobile = is_mobile(request)
request.ua_full = get_ua(request)
request.subdomain = get_subdomain(request)
return