0

I have a function defined as follows:

@app.route('/products', methods=['GET'])
@app.route('/products/<id>', methods=['GET', 'PUT'])
def foo():

  id = request.args.get('id', type=int)
  if id != None and request.method in ['GET', 'PUT']:
    # route to editing product template
  else:
    # display products as table

I have defined a Flask table like this:

class ProductsTable(Table):
  id=Col('ID', show=False)
  price=Col('Price')
  available_online=Col('Available online?')
  available_num=Col('In stock')
  edit=ButtonCol('Edit', url_kwargs=dict(id='id'), endpoint='/products')

I query an SQLite DB and fill the table. Displaying it works fine however I am struggling with the editing functionality.

Right now the way it works once the user presses the Edit button the URL changes to http://localhost:5000/products/1, http://localhost:5000/products/2 etc. depending on the ID of the item in the DB and respectively in the table (since it just displays the DB data).

However I would like to find out if it's possible to pass the ID as an URL parameter so that the URL for editing a specific product by pressing the Edit button becomes http://localhost:5000/products?id=1 http://localhost:5000/products?id=2 etc.

I tried passing '/products/<id>' to the endpoint argument but I get the following error

werkzeug.routing.BuildError: Could not build url for endpoint '/products/<id>' with values ['id']. Did you mean 'products' instead?

I also tried '/products?<id>', '/products?id=<id>' and also without the / in front of products but the result was the same. The only valid endpoint here is just products.

Is this possible and if yes, how can I achieve this behaviour?

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161

1 Answers1

1

I use the following format:

edit = LinkCol('Edit', 'app.foo', url_kwargs=dict(id='id'))

Equivalent to url_for('app.foo', id=id)

VERNSTOKED
  • 107
  • 1
  • 1
  • 10