0

I've read over many sources and still can't figure this out. Any help why I get this jinja2.exceptions.TemplateNotFound error would be appreciated. I am running this inside Atom, if that helps.

Directory:

static/    
templates/
    about.html
    home.html
app.py

Code:

from flask import Flask, render_template, jsonify

app = Flask(__name__)

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

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

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

2 Answers 2

0

Try this, you missed methods: GET, POST

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/', methods=['GET','POST'])
def home():
    return render_template('home.html')

@app.route('/about/', methods=['GET','POST'])
def about():
    return render_template('about.html')

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

home.html:

<body>
{% block body%} 
    <h1>Home Page</h1>
    <a href="{{url_for('about')}}">About Page</a>
{% endblock %}
</body>

about.html:

{% block body%} 
    <h1>About Page</h1>
{% endblock %}
3
  • Thanks for the reply. Unfortunately, still does not work. I even created a new project folder in Atom with just the app.py and home.html files in the same directory schema in my original post. Any other ideas?
    – Bryant
    Jun 11, 2020 at 6:40
  • I did. So I did more digging and altered your code to this: if name == 'main': port = int(os.environ.get('PORT', 5000)) app.run(host='192.168.1.10', port=port) Now if I run this with your setup, with the entire project directory on my desktop it works. But for some reason when I copy the exact same setup over to my Github directory it doesn't
    – Bryant
    Jun 11, 2020 at 7:28
  • @Bryant, can you send link to github repository, so I can check it fully?
    – user13628015
    Jun 11, 2020 at 8:31
0

For whatever reason, I can't run this through Atom's script package. I have to run through cmd.

Navigated to the folder with app.py and ran

python app.py

EDIT (FINAL SOLUTION): My problems were twofold.

1) In order to run on localhost, I added this bit of code from Flask render_template. (I used my IPv4 address instead of 0.0.0.0)

if __name__ == '__main__':     
     port = int(os.environ.get('PORT', 5000))     
     app.run(host='0.0.0.0', port=port)

2) In Atom's script package, there is a setting to set the current working directory. I guess the default is "First project directory" (what....) Changed this to "Directory of the script" fixed my issues.

Thanks everyone for the help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.