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)
Bryant
  • 85
  • 2
  • 8
  • seems like no issue with code. make sure that you were running the correct file. – NavaneethaKrishnan Jun 10 '20 at 08:08
  • Try to look here https://stackoverflow.com/questions/23327293/flask-raises-templatenotfound-error-even-though-template-file-exists – balderman Jun 10 '20 at 08:11
  • After using the accepted answer from here: https://stackoverflow.com/questions/44607950/flask-render-template/44608021 I am able to successfully run if the project folder is on my desktop. However it does not work in a Github repo (folder I store in C:/.../Documents) – Bryant Jun 11 '20 at 07:12

2 Answers2

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 %}
  • 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 '20 at 06: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 '20 at 07:28
  • @Bryant, can you send link to github repository, so I can check it fully? –  Jun 11 '20 at 08: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.

Bryant
  • 85
  • 2
  • 8