0

I'm working on Flask and I'm stuck in a strange situation. I'm trying to change an image with javascript but flask gives me these errors:

... 12:05:34] "GET / HTTP/1.1" 200 -

... 12:05:38] "GET /img/immagine.jpg HTTP/1.1" 404 -

JS:

var btnAddImage = document.getElementById("btnAddImage");
btnAddImage.onclick = function() {
  var img = document.createElement("img");
  img.src = "/static/img/immagine.jpg.jpg";
  document.getElementById("asi").appendChild(img);
};

As you can see in the path it's specified as static but it's not read.

I'm using the simply render_template method:

from flask import Flask, render_template
app = Flask(__name__)


@app.route("/")
def hello():
    return render_template('index.html', nome=nome)
    enter code here

1 Answer 1

1

The error message clearly states that the image could not be found.

Your code does not match the error message. One time you have .jgp as file ending, one time .jpg.jpg.

Try to compare the image name and path with the one you write in your code.

If you cannot figure out how to fix this, please also give us an overview of your directory structure.

Sign up to request clarification or add additional context in comments.

5 Comments

sorry it's an error in the vary test but it don't resolve the problem. the structure is project(static(img(svg1,svg2,img1,img2,img3)script.js,jquery,main.css)templates(page1,page2)app.py)
There is still no immagine.jpg in your project structure - either that is the problem or you want to make it really suprising for those who want to help you :-)
Is your code public available e.g on GitHub? Then I could clone your project and have a look at my machine.
thanks for help I think that depends by pycharm or chrome because when i change the name of the javascript file it change but i don't know why now
EDIT: with firefox works this is how I point to JS file <script type="text/javascript" src="{{ url_for('static',filename='ilmioscript.js') }}"></script>. If you prefer the project tell me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.