Fernker
Fernker

Reputation: 2288

Django mobile and full site redirects

I've been searching for a while on Google and haven't been able to find exactly what I'm looking for and decided to ask everyone here.

I've got a full site and mobile version of the site (one page jQuery Mobile file).

In theory what I would like is to check the incoming request for a possible mobile device (I can already do this) and redirect to the mobile site. On the mobile site the user can click a link to the full site that sets a cookie and won't redirect that user back to the mobile site (even though they will meet that condition with every request with their device).

All that I have been able to find is about how to check every request for mobile/desktop device and change the template accordingly. Which isn't what I need to do in this situation. It's a completely different template for the mobile and desktop versions.

Any ideas or solutions that may help in this situation?

UPDATE 1:

So I've tried implementing this and I am having some weird issues/problems. Here is my Middleware (listed last on the Middleware list in the settings)

class mobile_check(object):

def process_request(self,request):
    if mobileBrowser(request) and not request.session.get('view_full'):
        return HttpResponseRedirect(reverse('posts.views.mobile_django'))

Here's the view to handle when they click the full site link in jQuery Mobile.

def mobile_fullsite(request):
#import pdb; pdb.set_trace()

request.session['view_full'] = True
return HttpResponseRedirect(reverse('posts.views.index'))

In my head this should work fine but it doesn't. It hits the mobile_fullsite view from the link and the url changes from .../mobile/ to .../fullsite/ Yet it still looks like jQuery mobile with some stuff from the redirected pages.

Any ideas?

Upvotes: 1

Views: 3820

Answers (2)

brian buck
brian buck

Reputation: 3454

You might want a full redirect rather than just changing the template. Look at django.http.HttpResponseRedirect

from django.http import HttpResponseRedirect

# test for mobile device here
# test for full site session variable here
fullSiteRequest = request.session.get('User_Wants_Full_Site', False)

if mobileDevice and not fullSiteRequest:
    return HttpReponseRedirect('mobile.mysite.com')

# continue onto your regular site

ed. To reflect my comment below.

Upvotes: 0

dm03514
dm03514

Reputation: 55972

django-mobile has many of the features you are looking for. THey are implemented in a couple different parts. The first one detects the mobile user agent. The second one renders the template corresponding to that (either full site or mobile site). In addition it allows a user to select which version they want to see (with a GET) request. https://github.com/gregmuellegger/django-mobile

Please check it out, even if it doesn't have everything you need it has a wide following and i'm sure it will be able to offer you some code.

If your use case is a little different you can easily use django's sessions framework (https://docs.djangoproject.com/en/dev/topics/http/sessions/) to keep direct the user accordingly.

# user asks to view full site
request.session['view_full'] = True

then in your middle ware when checking for mobile user agent, psuedo below

if is_mobile and not request.session.get('view_full'):
  # they have mobile device and haven't request to see full site!~
  # Just check that they don't have a preference for viewing full site^

Upvotes: 1

Related Questions