Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, what is the best way to manage both a mobile and desktop site?

I'd like everything to function correctly, except when it's mobile, the entire site will used a set of specific templates.

Also, I'd like to autodetect if it's mobile. If so, then use that set of templates throughout the entire site.

like image 203
TIMEX Avatar asked Mar 07 '10 16:03

TIMEX


3 Answers

Have two sets of templates, one for mobile, one for desktop. Store the filenames in a pair of dictionaries, and use the User-agent header to detect which set should be used. Also allow manual selection of which site to use via a session entry.

like image 155
Ignacio Vazquez-Abrams Avatar answered Nov 14 '22 19:11

Ignacio Vazquez-Abrams


If you place a class on your body (Django uses something similar to specify what column style to use), you could use the same templates but simply use different stylesheets. I'm not sure what main differences you are using separate templates for, but this might allow you to cut down on re-coding the templates multiple times.

like image 1
Adam Avatar answered Nov 14 '22 18:11

Adam


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>
like image 1
Thomas Avatar answered Nov 14 '22 19:11

Thomas