1
我想爲特定版本的Django站點提供不同的(移動)瀏覽器。 有什麼可能的解決方案來做到這一點?如何爲不同的瀏覽器使用不同的模板
我想爲特定版本的Django站點提供不同的(移動)瀏覽器。 有什麼可能的解決方案來做到這一點?如何爲不同的瀏覽器使用不同的模板
在你看來,你smthg這樣
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
})
,並在您的模板
{% 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 %}
最佳實踐:使用minidetector到額外的信息添加到請求,然後使用Django的內置請求上下文將其傳遞到您的模板中。
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>
[基於用戶代理更改Django模板(可能重複http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-agent) – Eduardo 2014-05-07 02:29:29
似乎這樣已經在這裏回答了:[http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-代理](http://stackoverflow.com/questions/164427/change-django-templates-based-on-user-agent) – 2009-05-29 14:49:46