32

I have a web application written in Django that has one specific page I'd like to implement a mobile version of the template (and slightly different logic) for. I'd like to be able to implement it ala this sudo code:

def(myView)

  do some stuff

  if user-is-on-a-mobile-device:
     do some stuff
     return (my mobile template)

  else:
     do some stuff
     return (my normal template)

I don't have a huge amount of time and I'm pretty early on in my coding learning curve :) - I found what looks to be a very powerful pluggable app called bloom for getting mobile device capablities - http://code.google.com/p/django-bloom/wiki/BloomDevice However it seems to make a request via JSON to get lots of device specs I don't need, which seems a bit inefficient to me.

Does anyone have a suggest simpler method? My detection doesn't need to be 100%, just iPhone, iPod, android, and mainstream devices...

Does the http_user_agent string have some kind of mobile flag I can check for?

Tristan Brotherton
  • 2,423
  • 5
  • 31
  • 37

3 Answers3

19

Update:

I just found: http://code.google.com/p/minidetector/

Which seems to do exactly what I want, I'm going to test now. Feel free to tell me i'm wrong!

Tristan Brotherton
  • 2,423
  • 5
  • 31
  • 37
  • Thank you for sharing that find. To me, what that middleware does looks to be a better solution than all the home-grown ones I've ever used to achieve similar purposes. – ayaz Nov 11 '10 at 08:01
  • 2
    Is that project up to date? It hasn't seen a change since Dec 2010. I was using https://github.com/shelfworthy/minidetector instead since it was actively developed. Unfortunately the author just told me that it isn't supported any more and he's been meaning to delete that github repo so I'm looking for a replacement. – Dan Benamy Nov 28 '11 at 20:49
  • If you check the network for minidector there are a couple of forks that aren't so stale -- https://github.com/saschwarz/minidetector/network – Alvin Sep 13 '12 at 10:38
15

best practice: use minidetector to add the extra info to the request, then use django's built in request context to pass it to your templates like so.

from django.shortcuts import render_to_response
from django.template import RequestContext

def my_view_on_mobile_and_desktop(request)
    .....
    render_to_response('regular_template.html', 
                       {'my vars to template':vars}, 
                       context_instance=RequestContext(request))

then in your template you are able to introduce stuff like:

<html>
  <head>
  {% block head %}
    <title>blah</title>
  {% if request.mobile %}
    <link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-mobile.css">
  {% else %}
    <link rel="stylesheet" href="{{ MEDIA_URL }}/styles/base-desktop.css">
  {% endif %}
  </head>
  <body>
    <div id="navigation">
      {% include "_navigation.html" %}
    </div>
    {% if not request.mobile %}
    <div id="sidebar">
      <p> sidebar content not fit for mobile </p>
    </div>
    {% endif %>
    <div id="content">
      <article>
        {% if not request.mobile %}
        <aside>
          <p> aside content </p>
        </aside>
        {% endif %}
        <p> article content </p>
      </aricle>
    </div>
  </body>
</html>
Thomas
  • 11,032
  • 4
  • 37
  • 53
  • 1
    Many people follow this convention, though I don't like it because it forces the developers to have a multitude of if statements instead of just 1. You can argue that showing different templates altogether is not DRY but I think the sanity of separating the two is worth it. – Mikhail Sep 15 '13 at 02:10
7

go for the fork of minidetecor called django-mobi, it includes documentation on how to use it.

https://pypi.python.org/pypi/django-mobi

gterzian
  • 517
  • 6
  • 5
  • This seems like the best solution. __"Fork Description: I reorganized the code, added caching, and made a few tweaks here and there."__ – Druska Apr 22 '13 at 13:13