-1

I reduced the code for this to, I think, minimum while trying to get it to work: The python:

#!/usr/bin/env python
from functools import wraps
from flask import Flask, render_template, session, request, redirect, url_for
from flask_socketio import SocketIO, emit, join_room, leave_room, \
    close_room, rooms, disconnect


async_mode = None

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)

@app.route('/')
def index():
    return render_template('index_test.html')


if __name__ == '__main__':
    socketio.run(app, debug=True)

The html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <img src="{{ url_for('index') }}bulboff.gif"/>
    <p>"{{ url_for('index') }}" <p>
</body>
</html>

The image is in the static folder.

And it gives this error:

"GET /bulboff.gif HTTP/1.1" 404

when the page is accessed.

I've tried several things like setting the Flask default paths, without the url_for, etc, but, still no image.

What am I missing?

drmacro
  • 51
  • 4

3 Answers3

1

According to the flask document:

To generate URLs for static files, use the special static endpoint name:

url_for('static', filename='style.css')

The file has to be stored on the filesystem as static/style.css.

In your case, use <img src="{{ url_for('static', filename='bulboff.gif') }}">

nalzok
  • 14,965
  • 21
  • 72
  • 139
0

If You put image in the static folder, You should use something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <img src="{{ url_for('index') }}static/bulboff.gif"/>
    <p>"{{ url_for('index') }}" <p>
</body>
</html>

Alternatively, You could change application's code and add following:

@app.route('/bulboff.gif')
def bulboff():
    return open('static/bulboff.gif').read()
Fejs
  • 2,734
  • 3
  • 21
  • 40
0
<img src="{{url_for('static', filename='bulboff.gif')}}" />

Try that. Your filename could be a path from the static folder. So like if you have filename = \some\path\img.png it will look for the img in static\some\ath\img

MooingRawr
  • 4,901
  • 3
  • 24
  • 31