Flask:访问视图出现问题(服务器上未找到URL)

4

我正在尝试将另一个视图添加到我的Flask应用程序中。我的app/views.py文件看起来像这样:

from flask import render_template
from app import app
from helpfulFunctions import *

def getRankingList():
    allPlayers = main()
    return allPlayers

def displayLimitedNumberOfPlayers(limit):
    allPlayers = main()
    allPlayers[0] = limitPlayers(allPlayers[0], limit)
    allPlayers[1] = limitPlayers(allPlayers[1], limit)
    return allPlayers

@app.route("/")
@app.route("/index")
def index():
    rankingList = getRankingList()
    return render_template('index.html', title='Home', rankingList = rankingList)

@app.route("/top100")
def top100():
    rankingList = displayLimitedNumberOfPlayers(100)
    return render_template('top100.html', rankingList = rankingList)

if __name__ == '__main__':
    app.run(debug=True)

我试图模仿Miguel Grinberg教程中如何定义//index路由的方法。我在我的模板文件夹中创建了一个名为top100.html的视图,其中也包含index.html文件。然而,当我尝试访问localhost:5000/top100.html时,我收到以下错误信息:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

看起来 Flask 认为这个 URL 没有与之关联的视图……但我不确定为什么。

你有什么想法吗?

感谢帮助, bclayman


1
try localhost:5000/top100 - itzMEonTV
1
或者在你的路由中添加HTML @app.route("/top100.html") - Boris
2个回答

9

你的代码中没有 top100.html 视图。你可以选择以下任一操作:

localhost:5000/top100

@app.route("/top100")更改为@app.route("/top100.html")

3
路由(或网址)在 @app.route() 定义中指定,所以您应该访问 localhost:5000/top100top100.html 是在 Flask 内部引用的 render_template,用于指定使用的模板。实际上,此页面可以命名为任何名称,并且不必以与路由类似的方式命名...它只需与用于构建在该网址提供的页面的模板文件匹配即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接