【发布时间】:2021-09-17 23:33:56
【问题描述】:
我正在学习 Flask,我创建了两条路线:
- "/about" : 将模板路由为 render.template(about.html)
- "/aksh" : 将模板路由为 render.template(index.html)
它适用于index.html 而不是about.html,当我在index.html 中进行更改并刷新时,什么也没发生。
我尝试重新启动一切,包括我的系统。
然后我评论了那条路线“/aksh”,但令我惊讶的是,portaddress/about 没有显示 url,而 portaddress/aksh 仍在显示文本。
之后我重新编辑了整个代码,现在在新代码中有 3 条路线:
- “/”:返回打印语句“Hello World!”
- "/aksh" : 渲染模板
index.html - "/about" : 渲染模板
about.html
但是现在这并没有改变,我也得到了旧的结果,新的路线没有得到更新。
我的python代码是:
from flask import Flask,render_template
app=Flask(__name__)
@app.route("/")
def hello():
return "Hello World"
@app.route("/aksh")
def aksh():
return render_template('index.html')
@app.route("/about")
def about():
return render_template('about.html')
app.run(debug=True)
index 和 about 的 HTML 代码是: index.html:
<html>
<head>
<meta charset="utf-8">
<title>Title of my page</title>
</head>
<body>
<p>We are testing flask app here</p>
</body>
</html>
对于 about.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>About this page</title>
</head>
<body>
<h1>This is about section</h1>
</body>
</html>
它显示“我们正在测试应用程序”但它必须是“我们正在测试应用程序here”而且它的位置错误它必须在路线“/aksh”而不是“/”
【问题讨论】:
-
您是否保存了文件。按 shift+f5 尝试重新加载。最好在 if name == main 范围内运行 app。 codementor.io/@overiq/basics-of-flask-fzvh8ueed
标签: python flask flask-sqlalchemy