Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem about 500 Internal Server Error in Python Flask [closed]

This is my Python code:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/profile/<name>")

def profile(name):
  return render_template("index.html", name=name)

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

and HTML code:

<!DOCTYPE html>
<html>
    <head>

    </head>

    <body>
        Hello {{ name }}
    </body>
</html>

And when I run the Python code, it shows on the browser that:

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

I looked for the solution on Google as well as Youtube, but still can't fix it. Can someone help me with this? Thank you

Edit: so all I need to do is to fix this one line:

app = Flask(__name__, template_folder="template")
like image 814
Michael Nguyen Avatar asked Oct 29 '25 10:10

Michael Nguyen


2 Answers

Whenever we receive 500 internal server error on a Python wsgi application we can log it using 'logging'

First import from logging import FileHandler,WARNING

then after app = Flask(__name__, template_folder = 'template') add

file_handler = FileHandler('errorlog.txt')
file_handler.setLevel(WARNING)

Then you can run the application and when you receive a 500 Internal server error, cat/nano your errorlog.txt file to read it, which will show you what the error was caused by.

like image 196
Ahmed Rauf Khan Avatar answered Oct 30 '25 23:10

Ahmed Rauf Khan


  1. You must not had an empty line beetween @app.route("/profile/<name>") and def profile(name):

  2. You have to set the html file in a folder called templates.

  3. You have to set the templates folder and run.py in the same folder

like image 31
Charles R Avatar answered Oct 30 '25 23:10

Charles R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!