我有一个定义如下的函数:
@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
我已经定义了这样的Flask表:
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')
我查询一个SQLite数据库并填写表格。显示它可以正常工作,但是我在编辑功能方面苦苦挣扎。
现在,一旦用户按下“编辑”按钮,URL就会更改为http://localhost:5000/products/1
,http://localhost:5000/products/2
等,具体取决于数据库和表中项目的ID(自它只显示数据库数据。
但是我想知道是否可以将ID作为URL参数传递,以便通过按“编辑”按钮来编辑特定产品的URL变为http://localhost:5000/products?id=1
http://localhost:5000/products?id=2
等。
我尝试将'/products/<id>'
传递给endpoint
参数,但出现以下错误
werkzeug.routing.BuildError:无法为端点
'/products/<id>'
生成值为['id']
的URL。您是说'products'
吗?
我也尝试了'/products?<id>'
,'/products?id=<id>'
,并且在/
前面也没有products
,但是结果是相同的。这里唯一有效的端点就是products
。
这有可能吗,如果可以,我如何实现这种行为?
答案 0 :(得分:0)
我使用以下格式:
edit = LinkCol('Edit', 'app.foo', url_kwargs=dict(id='id'))
相当于url_for('app.foo', id=id)