2009-05-29 11 views
1

別の(モバイル)ブラウザ用の私のdjangoサイトの特別なバージョンをお届けしたいと思います。 これを行うにはどうすればよいですか?あなたのビューで別のブラウザで別のテンプレートを使用する方法

+0

[のUser-Agentに基づいて変更Djangoのテンプレート]の可能複製(http://stackoverflow.com/questions/164427/change-django-templates-on-user-agent) – Eduardo

+0

このように見えます:[http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-エージェント](http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-agent) –

答えて

1

、この

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 
     }) 

のようにして、テンプレートにsmthgん

{% 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 %} 
+0

私は投票したいです...しかし、私はまだ – user114337

+0

できません問題:D SOへようこそ – vikingosegundo

0

のベストプラクティス:その後、Djangoの要求に建て使用し、リクエストに余分な情報を追加するためにminidetectorを使用コンテキストをテンプレートに渡すことができます。

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)) 

その後、テンプレートにあなたのようなものを導入することができます:

<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> 
関連する問題