烧瓶上的错误 404

斯里扬·戈亚尔

当我运行托管在 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 个选择:

  • 更改链接,使 URL 为http://127.0.0.1:5000/article/business/003.txt通过这样做,您将能够保持与当前相同的路线@app.route("/article/<topic>/<filename>")这里topic将具有 的值"business"filename将具有 的值"003.txt"
  • 或者,您可以保留链接以使 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"

您可以在此处找到有关路线的更多信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章