当我运行托管在 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,但该文件存在于本地的特定路径中目录我相信错误在第二个代码片段中。我是烧瓶的初学者,所以我真的很困惑该怎么做。任何帮助将不胜感激
如果我理解正确,您正试图在路线中抓住topic
和filename
。问题是您尝试访问的 URL 与您定义的路由不匹配。
您有 2 个选择:
@app.route("/article/<topic>/<filename>")
。这里topic
将具有 的值"business"
和filename
将具有 的值"003.txt"
。@app.route("/article/data/bbc/<topic>/<filename>")
。再次topic
将具有 的值"business"
并且filename
将具有 的值"003.txt"
。您可以在此处找到有关路线的更多信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句