0

I am trying to use Flask to render an HTML template. I had it working great and now each time I get a 500 Internal Server Error. If I replace the render_template function just a string, things work fine. What am I doing wrong?

init.py :

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def homepage():
    return render_template("main.html")

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

main.html in /templates/

<!DOCTYPE html>
<html lang="en">
<p>test</p>
</html>
user2242044
  • 8,803
  • 25
  • 97
  • 164

2 Answers2

4

The template_folder has to be defined where the static files are located.

If the main.html file is in the same folder as the init.py, then include the following code:

import os

project_root = os.path.dirname(__file__)
template_path = os.path.join(project_root, './')

app = Flask(__name__, template_folder=template_path)

Hopefully it works now.

Biranchi
  • 16,120
  • 23
  • 124
  • 161
1

Your sample actually works on my end.

  • What version of flask are you running?

  • are you sure that you are accessing the URL at port 5000 (the default) and not an application on port 80?

  • Are old instances of the server still running, that may be colliding with attempts to re-launch the server?

thearn
  • 323
  • 3
  • 6
  • Flask Version: 0.10.1, URL that generates the Internal Server Error: http://127.0.0.1:5000/. I am using PyCharm and closed all connections. – user2242044 Jan 09 '16 at 20:39