如何在url\u for语句中添加/呢?

2024-09-23 22:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想说:转到additemtocart/1

<a href="{{url_for('additemtocart/1')}}">add to cart</a>

但我得到的只是一条错误信息:

werkzeug.routing.BuildError:

无法为终结点“additemtocart/1”生成url。你是说“额外的艺术”吗

python代码:

carts = []

def addtocart(item):
    carts.append({'name': checkusername(), 'items': item})
    print(carts)

@app.route('/additemtocart/<id>')
def additemtocart(id):
    if checklogin() == True:
        try:
            title = db.get_select_product(int(id))[5]
            addtocart(title)
            print(title + ' has been added to your shopping cart')
            return redirect(url_for('cart'))
        except:
            return 'item does not exist'
    else:
        return redirect(url_for('login'))```

Tags: toidurlforreturntitledefitem