I'd like to deliver special versions of my django site for different (mobile-)browser. What are possible solutions to do this?
            Asked
            
        
        
            Active
            
        
            Viewed 475 times
        
    1
            
            
        - 
                    possible duplicate of [Change Django Templates Based on User-Agent](http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-agent) – Eduardo May 07 '14 at 02:29
- 
                    Seems like this has already been answered here: [http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-agent](http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-agent) – Matthew Christensen May 29 '09 at 14:49
2 Answers
1
            In your view, do smthg like this
def map(request, options=None, longitude=None, latitude = None):
    if 'iPhone' in request.META["HTTP_USER_AGENT"]:
        user_agent = 'iPhone'
    elif 'MSIE' in request.META["HTTP_USER_AGENT"]: 
        user_agent ='MSIE'
    else: user_agent=''
    print user_agent
    return render_to_response('map/map.html', 
        {
            'user_agent': user_agent
        })
and in your template
{% ifnotequal user_agent "iPhone" %}
    {% ifequal user_agent "MSIE" %}
        {% include 'map/map_ie.html' %}
    {% else %}
        {% include 'map/map_default.html' %}
    {% endifequal %}
{% else %}
{% include 'map/map_iphone.html' %}
{% endifnotequal %}
 
    
    
        vikingosegundo
        
- 52,040
- 14
- 137
- 178
0
            
            
        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,757
- 4
- 41
- 57
