我有一个用Django编写的web应用程序,它有一个特定的页面,我想为它实现一个移动版本的模板(和略有不同的逻辑)。我希望能够通过这个sudo代码来实现它:
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)
我没有太多的时间,而且我的编程学习曲线还很早:) -我发现了一个名为bloom的非常强大的可插拔应用程序,用于获取移动设备的功能- http://code.google.com/p/django-bloom/wiki/BloomDevice然而它似乎通过JSON请求获取许多我不需要的设备规格,这对我来说似乎有点低效。
有谁有更简单的建议方法吗?我的检测率不需要是100%,只需要iPhone、iPod、安卓和主流设备就行了……
http_user_agent字符串是否有某种可以检查的移动标志?
发布于 2010-02-23 19:50:17
发布于 2010-11-11 07:48:13
最佳实践:使用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>
发布于 2013-03-19 14:50:05
找到名为django-mobi的minidetecor分支,它包含了如何使用它的文档。
https://stackoverflow.com/questions/2321172
复制