【问题标题】:`app.route` and `template_render` not working in Flask`app.route` 和 `template_render` 在 Flask 中不起作用
【发布时间】:2021-09-17 23:33:56
【问题描述】:

我正在学习 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 仍在显示文本。

之后我重新编辑了整个代码,现在在新代码中有 3 条路线:

  1. “/”:返回打印语句“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)

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”而不是“/”

【问题讨论】:

标签: python flask flask-sqlalchemy


【解决方案1】:

使用分隔符来解决这个问题。

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)

【讨论】:

    【解决方案2】:

    最后我设法解决了这个问题。 问题是我首先通过 PyCharm 运行这段代码,然后通过 Python 解释器 IDLE(显然是在关闭 pyCharm 之后)运行,并且不知何故 PyCharm 即使在系统重新启动后仍在控制服务器。

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

    所以结论是: ->即使在系统重新启动后,PyCharm 仍在控制服务器。 ->关机再开机为我解决了这个问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2018-04-03
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2020-10-24
      相关资源
      最近更新 更多