python 在python视图中检测移动浏览器(不仅仅是iPhone)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2321172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:21:58  来源:igfitidea点击:

Detect mobile browser (not just iPhone) in python view

pythondjangomobileuser-agent

提问by Tristan Brotherton

I have a web application written in Django that has one specific page I'd like to implement a mobile version of the template (and slightly different logic) for. I'd like to be able to implement it ala this sudo code:

我有一个用 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)

I don't have a huge amount of time and I'm pretty early on in my coding learning curve :) - I found what looks to be a very powerful pluggable app called bloom for getting mobile device capablities - http://code.google.com/p/django-bloom/wiki/BloomDeviceHowever it seems to make a request via JSON to get lots of device specs I don't need, which seems a bit inefficient to me.

我没有太多的时间,而且我的编码学习曲线还很早:) - 我发现了一个非常强大的可插拔应用程序,称为bloom,用于获取移动设备功能 - http://code. google.com/p/django-bloom/wiki/BloomDevice但是,它似乎通过 JSON 发出请求以获取我不需要的许多设备规格,这对我来说似乎有点低效。

Does anyone have a suggest simpler method? My detection doesn't need to be 100%, just iPhone, iPod, android, and mainstream devices...

有没有人建议更简单的方法?我的检测不需要100%,只需要iPhone、iPod、安卓和主流设备……

Does the http_user_agent string have some kind of mobile flag I can check for?

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

采纳答案by Tristan Brotherton

Update:

更新:

I just found: http://code.google.com/p/minidetector/

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

Which seems to do exactly what I want, I'm going to test now. Feel free to tell me i'm wrong!

这似乎完全符合我的要求,我现在要测试。随意告诉我我错了!

回答by Thomas

best practice: use minidetectorto add the extra info to the request, then use django's built in request context to pass it to your templates like so.

最佳实践:使用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))

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>

回答by gterzian

go for the fork of minidetecor called django-mobi, it includes documentation on how to use it.

寻找名为 django-mobi 的 minidecor 分支,它包含有关如何使用它的文档。

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

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