1

The problem

I am trying to run my first Flask applications while following the CS50 Javascript course. I have made succesfully implemented a few of the examples of the class. However I got a 500 Internal Server Error as I tried to run the following piece of code:

Code 1

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

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

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

I run the code in the terminal by calling the name of my python app as such:

python app.py

What I tried

1. I tried running a more simple example:

Code 2

from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
    return "hello world!"

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

Since I was able to run this without a problem I decided to get back to the previous example. However I haven't been able to figure out the solution. The syntax seems to be correct. To be sure I compared the code letter for letter with that of the video.

I noticed that the teacher of the video ran the application differently using:

set FLASK_APP = app.py
flask run

I tried that. This seemed to work too for a while.

Becoming frustrated I tried many of the things that worked before and even the simplest pieces of code such as code 2 seem to yield that 500 internal Server Error.

2. At some point I thought there might be a problem with flask which led me to reinstalling it.

What could be causing this? This is not the first time I have encountered this problem. Evertime it seems to "resolve" itself when I quit using the computer for a while which makes me none the wiser.

How do I find a solution online to a problem that I can merely define this broadly? The 500 Internal Server Error isn't really giving me hints.

[Edit:] I'm currently on a vacation at the hotel working on the local wifi. Could that have an impact on my server?

nid
  • 155
  • 3
  • 9
  • Is there any additional code in your editor/IDE besides the `flask` setup and route? – Ajax1234 Jul 21 '18 at 16:09
  • I'm not sure I understand your question. The pieces of code I pasted in my original question represent the total code for each script. Is this what you meant? – nid Jul 21 '18 at 16:12
  • Yes. Is there any error message displayed in the terminal? Try running with `app.run(debug=True)`. – Ajax1234 Jul 21 '18 at 16:14
  • There isn't an error message. I previously tried the app.run(debug=True). Didn't seem to affect my problem. I did just restart my computer and code 2 seems to work now. I have no idea what caused the fix. – nid Jul 21 '18 at 16:27
  • I solved it! After restarting the computer and running code 1 I finally got an error in my terminal. Googling the error gave me the solution: https://stackoverflow.com/questions/15053790/jinja2-exceptions-templatenotfound-error . My html files must apparently all be in the templates folder – nid Jul 21 '18 at 17:07

0 Answers0