0

I am trying to call some functions and return the results in the browser but I get a 500 Internal Server Error. I have this code following this answer

from flask import  Flask

app = Flask(__name__)

@app.route("/")
def message():
  return "Message from python function named 'message' "

@app.route("/user/<username>")
def user(username):
  return "Username is %s " % username

@app.route("/userpass/<username>/<password>")
def userpass(username, password):
  return "User is %s and password %s " % username % password

and I am calling the first one just by using

http://127.0.0.1:5000

the second one using

http://127.0.0.1:5000/user/test

and the last one by using

http://127.0.0.1:5000/userpass/test/123

and I get the error 500. What's the problem with the last call?

Cheetara
  • 165
  • 1
  • 5
  • 14

2 Answers2

4

Your return statement in the failing route should look like this:

return "User is %s and password %s " % (username, password)

naechtner
  • 110
  • 7
  • Is there a way to overcome the "self" parameter error with flask? I am getting a missing argument error while calling the function because of the self parameter I have to use which flask takes it as an extra argument – Cheetara Apr 27 '18 at 09:52
3

Python 2

The accepted answer works. However, %-formatting method is old and is not best practice.

The best practice is to use the format() method, like so:

return "User is {} and password {}".format(username, password)

Python 3.6+

f-Strings is the way to go

return f"User is {username} and password {password}"
AArias
  • 2,558
  • 3
  • 26
  • 36