32

我有一个用 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、android、主流设备……

http_user_agent 字符串是否有某种我可以检查的移动标志?

4

3 回答 3

20

更新:

我刚刚发现:http ://code.google.com/p/minidetector/

这似乎正是我想要的,我现在要测试。随意告诉我我错了!

于 2010-02-23T19:50:17.293 回答
15

最佳实践:使用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>
于 2010-11-11T07:48:12.570 回答
8

选择名为 django-mobi 的 minidetecor 的分支,它包含有关如何使用它的文档。

https://pypi.python.org/pypi/django-mobi

于 2013-03-19T14:50:05.317 回答