检查设备在Flask中是否可移动

时间:2019-03-27 10:46:37

标签: javascript css iframe flask mobile

我正在使用Flask和Materialize显示PDF文件列表

<table class="highlight responsive-table">
    <thead>
        <th class="left-align"><i class="material-icons">call</i></th>
        <th class="left-align"><i class="material-icons">email</i></th>
        <th class="center-align"><i class="material-icons">picture_as_pdf</i></th>
    </thead>
    <tbody>
        {% for doc in docs %}
            <tr>
                <td>{{doc.phone if doc.phone}}</td>
                <td>{{doc.email if doc.email}}</td>
                <td>
                    <a href="#modal{{loop.index}}" class="modal-trigger"><i class="material-icons">open</i> Ouvrir</a>
                </td>
            </tr>
            <div class="modal" id="modal{{loop.index}}">
                <iframe src="/cv/{{doc.name}}" scrolling="no" width="100%" height="100%"></iframe>
            </div>
        {% endfor %}
    </tbody>
</table> 

enter image description here

使用iframe在模式窗口中显示PDF。

enter image description here

当我在移动设备中打开页面时,没有以模态显示PDF,而是提示我下载pdf,就好像我试图直接下载它一样。当我在Flask中使用for循环时,我得到每个PDF的下载提示。我想知道是否有一种方法可以检查用户代理是否可移动,因此,在这种情况下,我将显示pdf链接而不是模式窗口。

1 个答案:

答案 0 :(得分:1)

我使用Flask-Mobility解决了这个问题。它可以检测设备是否可移动

from flask_mobility import Mobility

...

app = Flask(__name__)
Mobility(app)

...

{% for doc in docs %}
    <tr>
        <td>{{doc.phone if doc.phone}}</td>
        <td>{{doc.email if doc.email}}</td>
        <td>
            {% if request.MOBILE %}
                <a href="/cv/{{doc.name}}" target="_blank"><i class="material-icons">open</i> Ouvrir</a>
            {% else %}
                <div class="modal" id="modal{{loop.index}}">
                    <iframe src="/cv/{{doc.name}}" scrolling="no" width="100%" height="100%"></iframe>
                </div>
                <a href="#modal{{loop.index}}" class="modal-trigger"><i class="material-icons">open</i> Ouvrir</a>
            {% endif %}
       </td>
    </tr>
{% endfor %}