`app.route` 和 `template_render` 在 Flask 中不起作用

`app.route` and `template_render` not working in Flask

我正在学习 Flask,我创建了两条路线:

  1. "/about" : 将模板路由为 render.template(about.html)
  2. "/aksh" : 将模板路由为 render.template(index.html)

它适用于 index.html 而不是 about.html,当我在 index.html 中进行更改并刷新时,没有任何反应。 我尝试重新启动一切,包括我的系统。

然后我评论说路由“/aksh”,但令我惊讶的是 portaddress/about 显示没有找到 url 而 portaddress/aksh 仍然显示文本。

在那之后我 re-edited 整个代码,现在在新代码中有 3 条路线 :

  1. "/" : return 打印语句“Hello World!”
  2. "/aksh" : 渲染模板 index.html
  3. "/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)

HTML index 和 about 的代码是: 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)

最后我设法解决了这个问题。 问题是 1st 我 运行 通过 PyCharm 编辑了这段代码,然后通过 Python 解释器空闲(显然在关闭 pyCharm 之后)并且不知何故 PyCharm 仍在控制服务器甚至在系统重启后。

因此,在检查完所有内容并获得线索后,我刚刚完全关闭系统电源并重新启动,然后 运行 应用程序仅通过 IDLE 解决了所有问题!

所以结论是: ->PyCharm 即使在系统重启后仍然控制着服务器。 ->关机再开机解决了我的问题。

听起来很奇怪,但这就是它的工作原理。 如果这里有人能解释为什么 PyCharm 即使在重启后仍然控制服务器会更好。