-1

I am trying out a Flask Tutorial on how to do something very basic but I cannot figure out how to reference the .html file in my Flask code in IPython/Windows setup.

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

@app.route('/', methods=['GET'])
def index():
    names = [{'name' : 'Rebecca'}]
    return render_template('layout.html', names=names, language='Python', lang=True, framework='Flask')

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

I have the layout.html file saved to a directory on my C:Drive and in IPython would I need to do a change directory command prior to running the Flask code? IE, my .html file lives here:

%cd "C:\Users\Documents\Python\flask\static"

I get a long IPython error and at the bottom it seems to be telling me the .html file cannot be found. Any ideas? Thanks-

    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: layout.html
127.0.0.1 - - [10/Apr/2018 11:54:09] "GET / HTTP/1.1" 500 -

UPDATE

This is the .html code:

<!DOCTYPE html>
<html>
    <head>
        <title>Flask Templates</title>
    </head>
    <body>
        <h1>Hello, {{ names.name }}</h1>

        {% if lang %}
        <p>The language your using is {{ language }}.</p>
        {% else %}
        <p>The framework your using is {{ framework }}.</p
        {% endif %}
    </body>
</html>
bbartling
  • 3,288
  • 9
  • 43
  • 88

1 Answers1

1

Your html file must reside inside the templates folder, which is inside your projects folder.

.
├── app.py
├── config.py
├── __pycache__
│   ├── config.cpython-34.pyc
│   └── sqlite3db.cpython-34.pyc
├── README
├── sqlite3db.py
├── static
│   └── favicon.png
├── templates
│   ├── home.html
│   └── login.html
└── users_db.lite
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
  • If I am running IPython do I need to save .py file to my project directory, then run it? Would there be any difference using IPython or not?? Thanks – bbartling Apr 10 '18 at 17:18
  • Humm, forgive me, please, but I never wanted to use IPython :'), so I cannot give any comment, @HenryHub, but, AFAIK, a FlaskApp should work independently of the environment which is providing its execution. What you have to be sure, is that: in the same folder where your application file resides, there must be a folder called `templates`, with all html files for your app :). If my answer was the one that you're looking for, please, I'd like to ask you to accept it. Regards. – ivanleoncz Apr 10 '18 at 17:40