【问题标题】:App Engine Python: Templates not rendering properlyApp Engine Python:模板未正确呈现
【发布时间】:2012-03-23 10:07:35
【问题描述】:

我不明白这段渲染页面的代码有什么问题。

def post(self):
        acct = self.request.get('account')
        pw = self.request.get('password')

        que = db.Query(User)
        que = que.filter('account =', acct)
        que = que.filter('password =', pw)
        results = que.fetch(limit = 1)

        values = { }
        newval = dict(values)
        newval['path'] = self.request.path

        if len(results) > 0:
            path = os.path.join(os.path.dirname(__file__), 'templates/sites.htm')
            self.response.out.write(template.render(path, {}))

我从一个名为 "loginscreen.htm" 的页面上的登录表单调用它。 当应用程序到达这部分代码时:

if len(results) > 0:
    path = os.path.join(os.path.dirname(__file__), 'templates/sites.htm')
    self.response.out.write(template.render(path, {}))

并尝试重定向到 'sites.htm',页面 'sites.htm' 正确显示,但在地址栏中仍显示:

'http://localhost:8080/login'(“/login”路由来自“loginscreen.htm”的传入请求)当'http://localhost:8080/sites .htm' 应该 改为显示。

这样做的主要问题是,如果我重新加载页面,“确认表单重新提交” 对话框会出现,允许用户再次提交表单。 但是如果我替换

path = os.path.join(os.path.dirname(__file__), 'templates/sites.htm')
self.response.out.write(template.render(path, {}))

self.redirect('sites.htm') 

地址栏正确显示'http://localhost:8080/sites.htm'

那么代码有什么问题呢?

对不起,如果我让这个问题听起来比应该的更复杂。

提前致谢!

【问题讨论】:

    标签: python google-app-engine django-templates


    【解决方案1】:

    您的问题在这里是一个误解:

    从名为“loginscreen.htm”的页面上的登录表单调用它。 当应用程序到达这部分代码时:

    if len(results) > 0:
        path = os.path.join(os.path.dirname(__file__), 'templates/sites.htm')
        self.response.out.write(template.render(path, {}))
    

    并尝试重定向到“sites.htm”,页面“sites.htm”是 正确显示,但在地址栏中仍然显示:

    该代码不会做任何类似于重定向的事情——它所做的只是告诉 AppEngine 通过呈现sites.htm 处的模板来响应该登录尝试以响应 POST 请求。如果你想重定向,你需要明确地这样做(你似乎已经尝试过)。

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 2015-04-01
      • 2015-06-18
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2020-09-06
      • 2010-10-02
      • 2014-04-23
      相关资源
      最近更新 更多