重定向到烧瓶中的不同路由

时间:2020-04-17 09:42:22

标签: python flask

我在烧瓶中有此路线:

@app.route("/welcome/<string:book_isbn>",methods=["GET", "POST"])
def book(book_isbn):

我有一个html文件,该文件具有以下链接:

<a href="{{ url_for('welcome',book_isbn=book.isbn)}}">

其中book.isbn是我在呈现此html文件时较早传递的变量。

当我点击链接希望实现该链接时,它将转到我提到的路线

但是它转到这条路线:

@app.route("/welcome",methods=['GET','POST'])
def welcome():

在顶部的Web浏览器中,我可以看到欢迎路线?book_isbn = xxxxxx(x只是一些数字)

所以我认为“?”是问题,但我无法确定是什么原因引起的。

1 个答案:

答案 0 :(得分:1)

您可能已经检查过,但是...是否确认book.isbnstring

?函数具有与现有路由不匹配的参数时,将出现url_for,因此该<string:book_isbn>存在问题。文件here

您可以尝试以下任一行:

type(book.isbn) # Should return "str"
isinstance(book.isbn, str) # Should return True

编辑

尝试使用可选参数将两条路由路由到一条路由中:

@app.route("/welcome",methods=['GET','POST'])
@app.route("/welcome/<string:book_isbn>",methods=['GET','POST'])
def book(book_isbn=None):
    if book_isbn:
        pass
    else:
        pass