【问题标题】:How can I redirect to a new page after submitting a form using Jinja template?使用 Jinja 模板提交表单后如何重定向到新页面?
【发布时间】:2018-03-01 16:10:24
【问题描述】:

我正在研究 Google 的 App Engine 留言簿示例(可在此处找到:https://cloud.google.com/appengine/docs/standard/python/getting-started/creating-guestbook

我正在尝试将输出(问候语)重定向到另一个页面,而不是在用户按下“签署留言簿”按钮后当前显示的 index.html 页面。我创建了一个名为 greetings.html 的单独页面,我从 index.html 页面复制了显示代码。但是,我不知道如何修改 guestbook.py 以使输出转到新页面。

【问题讨论】:

    标签: python google-app-engine jinja2


    【解决方案1】:

    webapp2 有一个内置的redirect 方法:

    return redirect('/some-path')

    但是,我认为您可能更愿意将收集到的数据发送到greetings.html 模板?在POST 方法下,你可以这样做:

    template_values = {
        'guestbook_name': guestbook_name,
        # etc.,
    }
    
    template = JINJA_ENVIRONMENT.get_template('greetings.html')
    self.response.write(template.render(template_values))
    

    【讨论】:

    • 感谢您的评论,但我不确定我是否知道如何合并它。您能否更具体地介绍一下手头的应用程序?我会将其合并到留言簿类下的 post 方法中吗?如果有,怎么做?
    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 2020-09-13
    • 2019-04-18
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多