0

In my very simle Flask app "rendare_template" is not working. What do I need to set, code or remove from my code?

main.py

import requests
from flask import (Blueprint, render_template, abort)
from flask import Flask
from jinja2 import TemplateNotFound
from flask import Flask
import simple_page
from simple_page import simple_page

app = Flask(__name__)
app.register_blueprint(simple_page)


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

simple_page.py

from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound

simple_page = Blueprint('simple_page', __name__, template_folder='templates')

@simple_page.route('/', defaults={'page': 'index'})
@simple_page.route('/<page>')
def show(page):
    try:
        return render_template('pages/%s.html' % page)
    except TemplateNotFound:
        abort(404)

When I run the code, I am getting:

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

1 Answers1

1

Flask will look for templates in the templates folder.

...
return render_template('pages/%s.html' % page)

You should create a pages folder inside templates folder and move your templates there.

So when you access http://127.0.0.1:5000/index, your code will look for index.html inside templates/pages

See this for more information.

johnny243
  • 304
  • 3
  • 13
  • If you read the article you sent me, you will see that there is not a single sentence related to PAGES folder, thank you Sir. – Ferid Š. Sejdović Sep 26 '19 at 15:51
  • There is no reference to the `pages` folder on the docs, but this is something you added on your code. It is up to you and it's valid if you want to organize your project that way. – johnny243 Sep 26 '19 at 15:54
  • I have a similar application, main application where I get the same error and I don't have a pages folder, nevermind, thank you anyway Sir. – Ferid Š. Sejdović Sep 26 '19 at 15:58