0

我有一个带有简单文本的页面和一个表单中的按钮

<html>
<head>
</head>
<body>
    <h1>JASON</h1>
    <form>
      <button type="submit" formmethod="POST">Activate</button>
      <br>
      <input type="hidden" value="act.12344" name="sub" />
    </form>
</body>
</html>

而这个python脚本

from flask import Flask, render_template, request, redirect

app = Flask(__name__)

@app.route('/atdt', methods=['GET', 'POST'])
def atdt():
    if request.method == 'POST':
        print('post')
        requested = request.form['sub']
        ver = str(requested).split('.')
        if ver[0] == 'act':
            print('act')
            modif(ver[1])                        #this func modifies the index page
            return render_template('index.html')

    else:
        return render_template('index.html')

脚本的重点是将名称 jason 更改为其他内容......而且效果很好,页面已更改,一切都很好

但是我的烧瓶程序不会显示它......“.html”页面已更改,如果我手动打开它,它可以在程序之外工作!

但是如果我给python这条线return render_template('index.html')但它不会呈现它如果我尝试手动刷新它只会向我显示旧页面

有什么帮助吗?

4

1 回答 1

0

您没有修改 html,您只是调用了一个返回输入修改版本的函数!

首先你必须使用模板引擎

你的 HTML 应该是这样的:

<html>
<head>
</head>
<body>
    <h1>{{name}}</h1>
    <form>
      <button type="submit" formmethod="POST">Activate</button>
      <br>
      <input type="hidden" value="act.12344" name="sub" />
    </form>
</body>
</html>

您的视图应如下所示:

@app.route('/atdt', methods=['GET', 'POST'])
def atdt():
    if request.method == 'POST':
        print('post')
        requested = request.form['sub']
        ver = str(requested).split('.')
        if ver[0] == 'act':
            print('act')
            name = modif(ver[1])                        #this func modifies the index page
            return render_template('index.html', name=name)

    else:
        return render_template('index.html', name="JASON")

模板引擎将处理名称更改

Flask 使用 Jinja2 模板引擎,你可以在这里阅读更多关于它的信息

于 2018-10-04T20:10:10.340 回答