0

I am creating a flask app and try to render index.html. When I run the app, these errors occurs:

127.0.0.1 - - [09/Oct/2020 15:08:16] "GET /dim0/image1.png HTTP/1.1" 404 -
...

Here is the code app.py:

...
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()

The app structure:

+-- app.py
+-- static
|   +-- dim0
|       +-- image1.png
+-- templates
|   +-- index.html
+-- Procfile
+-- runtime.txt
+-- requirements.txt
Hadif Hatta
  • 75
  • 1
  • 11

2 Answers2

3

Flask expects templates and NOT template as default folder's name to hold templates.

refer to https://flask.palletsprojects.com/en/1.1.x/api/?highlight=flask#flask.Flask

template_folder – the folder that contains the templates that should be used by the application. Defaults to 'templates' folder in the root path of the application.

if you want to rename that folder to template you need to mention that in app definition :

    app = Flask(
        __name__, 
        # static_url_path=None,
        # static_folder='static',
        # static_host=None,
        # host_matching=False,
        # subdomain_matching=False,
        template_folder='template',  # HERE
        # instance_path=None,
        # instance_relative_config=False,
        # root_path=None
    )

update:

maybe you need to remove the / at the beginning in

<img src="{{ url_for('static', filename='dim0/image1.png') }}" alt="..">
cizario
  • 3,995
  • 3
  • 13
  • 27
0

After almost a day of searching, the only thing I missed is this line static_url_path=''. Now it loads every file flawlessly.

app = Flask(__name__, static_url_path='')
Hadif Hatta
  • 75
  • 1
  • 11