How can I check if static file exists or not from url_for() in Flask?
Question:
Is there is way I can check the static file from url_for()
is really exist or not? Because I would like to check that if the first file does not exists, I would like to replace it with another file just like this in my html file:
{% set IconPng = url_for('static', filename="images/favo.png") %}
{% set IconIco = url_for('static', filename="images/favo.ico") %}
{% set Default = url_for('static', filename="images/default.ico") %}
{% if IconPng %}
<img src="{{IconPng}}" />
{% elif IconIco %}
<img src="{{IconIco}}" />
{% else %}
<img src="{{Default}}" />
{% endif %}
I have tried with above and also with or
operator like:
<img src="{{IconPng or IconIco or Default}}" />
However, it does not work. Any tip how can I do this check for url_for()
in Flask?
Thanks
Answers:
In my opinion it is rather task for javascript than your backend.
You can check if image exist using function like this one, and then change image src accordingly:
function file_exists(url){
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status != 404;
}
-
get path to static or template folder 2) concatenate with file name 3) check if file exist
from flask import Flask
from flask import render_template
app = Flask(name)
@app.route("/")
def main():
app.logger.debug(os.path.exists(os.path.join(app.static_folder, ‘staticimage.png’)))
app.logger.debug(os.path.exists(os.path.join(app.template_folder, ‘index.html’)))
return render_template(‘index.html’)
Is there is way I can check the static file from url_for()
is really exist or not? Because I would like to check that if the first file does not exists, I would like to replace it with another file just like this in my html file:
{% set IconPng = url_for('static', filename="images/favo.png") %}
{% set IconIco = url_for('static', filename="images/favo.ico") %}
{% set Default = url_for('static', filename="images/default.ico") %}
{% if IconPng %}
<img src="{{IconPng}}" />
{% elif IconIco %}
<img src="{{IconIco}}" />
{% else %}
<img src="{{Default}}" />
{% endif %}
I have tried with above and also with or
operator like:
<img src="{{IconPng or IconIco or Default}}" />
However, it does not work. Any tip how can I do this check for url_for()
in Flask?
Thanks
In my opinion it is rather task for javascript than your backend.
You can check if image exist using function like this one, and then change image src accordingly:
function file_exists(url){
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status != 404;
}
-
get path to static or template folder 2) concatenate with file name 3) check if file exist
from flask import Flask
from flask import render_template
app = Flask(name)
@app.route("/")
def main():
app.logger.debug(os.path.exists(os.path.join(app.static_folder, ‘staticimage.png’)))
app.logger.debug(os.path.exists(os.path.join(app.template_folder, ‘index.html’)))
return render_template(‘index.html’)