如何检查系统中是否存在文件或在 html 页面中不使用 jinja?

Rit*_*vda 7 html python jinja2 python-3.x

我想检查我的dummy.csv是否存在于我的本地系统中。如果存在,则将显示此检查欺诈状态,否则不会显示。我不想使用 JavaScript。如果有任何与 python jinja有关的解决方案,请帮助我。

还有一件事如何import os在 html 页面中使用 jinja?

我曾尝试过这个:

布局.html

{% if os.path.exists('./static/userdata/dummy.csv') %}
   <a class="nav-item nav-link" href="{{ url_for('checkfraudstatus') }}">Check Fraud Status</a>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

错误

jinja2.exceptions.UndefinedError: 'os' is undefined
Run Code Online (Sandbox Code Playgroud)

Rit*_*vda 5

将其添加到包含 import os 模块的 main.py 文件中。

主要.py

import os

@app.context_processor
def handle_context():
    return dict(os=os)
Run Code Online (Sandbox Code Playgroud)

布局.html

{% if os.path.exists('./static/userdata/dummy.csv') %}
     <a class="nav-item nav-link" href="{{ url_for('checkfraudstatus') }}">Check Fraud Status</a>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

这对我有用。希望它对其他人有帮助。