![](/img/trans.png)
[英]werkzeug.routing.BuildError: Could not build url for endpoint 'profile'. Did you mean 'index' instead?
[英]How to fix error: werkzeug.routing.BuildError: Could not build url for endpoint 'delete' with values ['id']. Did you mean 'index' instead?
我试图在 HTML 中有一个按钮,当我单击它时会删除数据库中的一行。 这是一个烧瓶应用程序。 这是 HTML:
<div class="container-fluid text-center" id="products">
{% for product in productList %}
<div class='userProduct'>
<a href="{{ product.productURL }}" target="_blank">{{ product.title|truncate(30) }}</a>
<h4 class="currentPrice">${{ product.currentPrice }}</h4>
<h5 class="budget">Budget: {{ product.userBudget }}</h5>
<form action="{{ url_for('delete', id=product.id) }}">
<button class="btn btn-sm btn-primary" type="submit">Remove from Wishlist</button>
</form>
</div>
{% endfor %}
</div>
这是python文件中的路由
@app.route('/delete/<id>')
@login_required
def delete(id):
remove_product = Product.query.filter_by(id=int(id)).first()
db.session.delete(remove_product)
db.session.commit()
return redirect(url_for('dashboard'))
我对 url_for 做的有什么问题吗? 我想将按钮中的 id 传递给 python 文件,以便我可以确定要从数据库中删除哪个条目。
您可能将 ID 作为整数传递,而您的路线需要一个字符串。 改用这个:
@app.route('/delete/<int:id>')
我刚刚遇到了同样的错误,就我而言,它是由数据库中的某些值为 None 引起的。 所以也许检查 product.id 是否有缺失值。
为了解决这个问题,我需要一个额外的行,对你来说看起来像这样:
@app.route('/delete/<id>') # current line
@app.route('/delete', defaults={'id': None}) # added line which handles None
在 delete(id) 中,您必须处理 id 为 None 的情况:
if id is None:
... # what should happen if id is None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.