Getting and using mobile data in your views and templates
Due to the differences in the implementation of each major carrier in JP, it is important that we know what carrier we are talking to in the views. Furthermore due to the cookieless environment of jp mobile, sessions are not usable in the django environment (django does not have a fallback feature that injects session ids into the URL to replace session via get, like PHP) Without sessions, there's nothing much you can do. mobiledjango sidesteps this by introducing fake cookies and implementing PHP like session ids injection. Below is sample view code and template of how you can use mobiledjango The viewBelow is a simple example of incrementing a value, inserting it into the sessions from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound, Http404
from django.views.decorators.cache import cache_control
from django.db import transaction
from mamopublic.mogo import mogodef
def main(request):
_TEMPLATE_PATH = "main.html"
response_dict = {}
# Test if client is a mobile
# We cannot use the sessions because if it's a first time access, there will be no sessions.
# Use the META data in the request instead
if not request.META.get(mogodef.CONST_MOGO_HTTP_META_CARRIER, None):
_TEMPLATE_PATH = "pconly.html"
return render_to_response(_TEMPLATE_PATH, RequestContext(request, dict=response_dict))
if not request.GET.get(settings.SESSION_COOKIE_NAME, None):
url = "/test01/?%s=%s&guid=ON" % (settings.SESSION_COOKIE_NAME, request.session.session_key)
return HttpResponseRedirect(url)
mogo_carrier = request.session[mogodef.CONST_MOGO_SESSION_DATA]['carrier']
mogo_uim = request.session[mogodef.CONST_MOGO_SESSION_DATA]['uim']
mogo_browser = request.session[mogodef.CONST_MOGO_SESSION_DATA]['browser']
mogo_locale = request.session[mogodef.CONST_MOGO_SESSION_DATA]['locale']
response_dict['sid'] = request.session.session_key
response_dict['sid_key'] = settings.SESSION_COOKIE_NAME
response_dict['mogo_carrier'] = mogo_carrier
response_dict['mogo_uim'] = mogo_uim
response_dict['mogo_browser'] = mogo_browser
response_dict['mogo_locale'] = mogo_locale
count = request.session.get('count', 0)
count += 1
request.session['count'] = count
response_dict['count'] = request.session['count']
return render_to_response(_TEMPLATE_PATH, RequestContext(request, dict=response_dict))
Points are - Import mogodef into your views:
from mamopublic.mogo import mogodef
- Use the sessions data below to access information concerning the mobile in your views:
request.session[mogodef.CONST_MOGO_SESSION_DATA]['carrier'] The carrier, SOFTBANK, DOCOMO or EZWEB
request.session[mogodef.CONST_MOGO_SESSION_DATA]['uim'] The unique user id. This will be unique for each terminal
request.session[mogodef.CONST_MOGO_SESSION_DATA]['browser'] The browser type, MOBILE or PC
request.session[mogodef.CONST_MOGO_SESSION_DATA]['locale'] The locale, i.e JP or US - Check if a client is a mobile or not by looking for the META carrier data in the request
if not request.META.get(mogodef.CONST_MOGO_HTTP_META_CARRIER, None): ...
The templateYou will need to specify the session ids in the template, to be processed by the mogo's middleware so that the framework recognizes sessions. <html>
sid: {{ sid }} <br>
<br>
COUNT: {{ count }} <br>
carrier: {{ mogo_carrier }} <br>
uim: {{ mogo_uim }} <br>
locale: {{ mogo_locale }} <br>
browser: {{ mogo_browser }} <br>
<a href=".?{{ sid_key }}={{ sid }}&guid=ON">GET LINK</a>
<form action=".?guid=ON" method="POST">
<input type="hidden" name="{{ sid_key }}" value="{{ sid }}">
<input type="submit" value="POST LINK">
</form>
</html>
|