如果用户试图通过get访问图书路由,如何呈现随机图书



我现在正在使用flask开发一个图书web应用程序。我想在图书部分为用户呈现随机图书。这意味着,如果用户试图通过get方法访问图书部分,我只会向他们呈现随机书籍。我已经搜索过了,我发现的都是随机方法,但我如何使用它

p.S:我正在使用谷歌图书API

没有代码这很难,但你应该做的是:

@app.route("/booksection")
def get_random_books():
all_books = get_books_from_api() # this function represents the way you get a the books from the API and returns a list. I don't know how you do that.
random_book = random.choice(all_books) # use random.choice to get 1 random element of a list 
return random_book

最新更新