NickP NickP - 3 months ago 11
Python Question

Passing id in route producing uncallables

I am trying to transition my workflow into Flask to write a simple web interface for a Python script.

However, doing the following, raises a type error constantly:

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

@app.route('/restart/<int:id>')
def restart(id):
return id

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


I would basically just like to show the id that is passed in the URL.

Am I missing something? This is exactly how I would do this in Django for example and all the examples on the Net have pointed to this approach in Flask.

Answer

Your route function should be returning a string but you're returning the integer you're passing into it. Cast it to a string instead:

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

@app.route('/restart/<int:id>')
def restart(id):
    return str(id)

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