【问题标题】:Redirecting to html page after submitting to flask server提交到flask服务器后重定向到html页面
【发布时间】:2020-12-24 23:07:46
【问题描述】:

我是烧瓶、html 和 java 脚本的新手。 我有一个 index.html 页面,可以呈现到另一个 html 表单页面。 我的问题是:当我单击提交按钮时,我想返回 index.html。 我找不到满足我需求的信息。 现在我有类似的东西:

<form action="http://127.0.0.1:5000/api/addClient" method="POST"> 
                <button type="submit"  class="btn btn-primary">Submit</button>
        
</form>

但这允许我被渲染到我的服务器页面。 我希望能够将我的表单提交到烧瓶服务器并呈现到 index.html 另外,我试过了:


@app.route('/api/debt')
def index():
    file = get_resent_file()
    debt_df = get_df(file)
    debt_json = debt_df.to_json(orient='records', force_ascii=False)
    ret = jsonify(json.loads(debt_json))
    return ret


@app.route('/api/addClient', methods=['GET', 'POST'])
def addClient():
    #things I do with data from submitted form...
    return redirect(url_for('index'))

但是当我这样做时,我被渲染为: 而且我可以看到一个 json 数据,而不是我的网页。

很乐意提供任何帮助!

【问题讨论】:

    标签: html flask rendering


    【解决方案1】:

    您将获得一个 JSON,因为当您转到 ('/api/addClient') 路由时,它会将您重定向到您的索引函数,该函数最终返回变量 ret,这是您屏幕上显示的 JSON。

    在您的索引函数中,将最后一个石灰更改为render_template("index.html")。在同一行中,如果您希望在 HTML 文件中使用 ret 变量,请将其更改为 render_template("index.html", ret=ret)

    【讨论】:

    • 感谢您的回复。我已经用 JS 方法解决了我的问题。不太优雅,但对我有用。
    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 2017-10-28
    • 2012-09-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多