检查设备是否在 Flask 中移动

Ami*_*udi 2 javascript css iframe mobile flask

我正在使用 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> 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

在此处输入图片说明

当我在移动设备中打开页面时,它会提示我下载 pdf,而不是在模态中显示 PDF,就像我试图直接下载它一样。当我for在 Flask 中使用循环时,我会收到每个 PDF 的下载提示。我想知道是否有办法检查用户代理是否是移动的,如果是这种情况,那么我将显示指向 pdf 的链接而不是模式窗口。

Ami*_*udi 7

我使用 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 %} 
Run Code Online (Sandbox Code Playgroud)