烧瓶上的错误 404

Error 404 on flask

当我 运行 托管在 127.0.0.1:5000 上的 server.py 时,它会生成文章列表

@app.route("/")
def articles():
    """Show a list of article titles"""
    return render_template('articles.html', my_list= Alist)

上面的代码生成了文章列表,当我 运行 127.0.0.1:5000 时 运行 正确地显示了列表。

@app.route("/article/<topic>/<filename>")
def article(topic,filename):
    """
    Show an article with relative path filename. Assumes the BBC structure of
    topic/filename.txt so our URLs follow that.
    """
    for art in articles_table:
        if art[0]== "%s/%s" %(topic, filename):
            title_str = art[1]
            text_list = art[2].split('\n')
            text_list = [t.lower() for t in text_list if len(t) >= 1]
            rec = recommended(art[0], articles_table, 5)
            break

   return render_template('article.html', title=title_str, text=text_list, 
fiveA= rec)

但是,每当我点击任何一篇文章时,它都会重定向到 http://127.0.0.1:5000/article/data/bbc/business/003.txt 并生成错误 404,但该文件存在于本地目录中的特定路径中 我相信错误出在第二个代码片段中。 我是烧瓶的初学者,所以我真的很困惑该怎么做。任何帮助将不胜感激

如果我没理解错的话,您正试图捕捉路线中的 topicfilename。问题是您尝试访问的 URL 与您定义的路由不匹配。

您有 2 个选择:

  • 更改 link,使 URL 为 http://127.0.0.1:5000/article/business/003.txt。通过这样做,您将能够保持与当前 @app.route("/article/<topic>/<filename>") 相同的路线。这里 topic 的值为 "business"filename 的值为 "003.txt"
  • 或者您可以保留 link,这样 URL 保持不变 (http://127.0.0.1:5000/article/data/bbc/business/003.txt),您可以将路线更改为如下所示:@app.route("/article/data/bbc/<topic>/<filename>")。同样,topic 将具有 "business" 的值,而 filename 将具有 "003.txt" 的值。

您可以找到更多关于路线的信息here