如何在html页面中检查文件是否存在于系统中



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

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

我试过这个:

layout.html

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

错误

jinja2.exceptions.UndefinedError: 'os' is undefined

将其添加到包含导入操作系统模块的main.py文件中。

主.py

import os
@app.context_processor
def handle_context():
return dict(os=os)

layout.html

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

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

检查文件是否存在的另一种方法是为此定义一个自定义函数。

from jinja2 import Template
import os

def path_exists(path):
return os.path.exists(path)

template = Template("""
{% if path_exists('template.html') %} 
File is found 
{% else %} 
File not found 
{% endif %}
""")
print(template.render(path_exists=path_exists))

相关内容

最新更新