python - `app.route` 和 `template_render` 在 Flask 中不起作用
问题描述
我正在学习 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>
它显示“我们正在测试应用程序”,但它必须是“我们正在测试应用程序”,而且它的位置错误,它必须在路线“/aksh”而不是“/”
解决方案
使用分隔符来解决这个问题。
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)
推荐阅读
- javascript - “无法读取属性” [JS]
- python - 在 Python 中使用 __name__ 语句的区别
- python - 如何在 discord.py 中获取用户活动?
- database - 使用 javascript 和协调在网站上显示文件
- excel - 根据其他 2 种单元格颜色更改单元格颜色
- arrays - 从 threejs 几何体中移除/拼接面(版本 r125 及更高版本)
- javascript - 将外部配置文件添加到 Vue.js 打字稿
- python - 在没有键的循环中使用 dict 值更新 csv
- python-3.x - 我如何能够修改甚至不在该类内的函数内的对象属性
- google-api - 是否有可以订阅的 Google webhook/push 通知 API,以便在特定 Google 文档被丢弃时收到通知?