【问题标题】:Google App Engine Python: Redirect to same page to reload tableGoogle App Engine Python:重定向到同一页面以重新加载表
【发布时间】:2015-10-10 20:36:16
【问题描述】:

我有一个 html 页面,您可以在其中在表单中输入信息,当您单击提交按钮时,它会重定向页面并使用新信息更新表格。我使用了“self.redirect('/')”,它确实重新加载了所有内容,但没有更新表格,除非我手动刷新页面。以下是我的代码:

#Used to load up index.html    
class MainHandler(webapp2.RequestHandler):
    #HTML Get request to pull information from the datastore
    def get(self):
        title = "Video Game History"
        template_vars = {
            'title' : title, 'message' : "Enter the video game that you've played before..." }
        template = JINJA_ENVIRONMENT.get_template('index.html')
        self.response.out.write(template.render(template_vars))

        #Pulls info from the datastore and displays it
        self.query = MyGames.query()
        self.response.write("""
            <table>
                <caption>Game History</caption>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Company</th>
                        <th>Console</th>
                        <th>Genre</th>
                        <th>Recommend</th>
                    </tr>
                </thead>
                <tbody>         

            """)
        for dbInfo in self.query:
            self.response.write("""
                <tr>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                </tr>
                """ % (dbInfo.name, dbInfo.description, dbInfo.company, dbInfo.console, dbInfo.genre, dbInfo.recommend))

        self.response.write("""
                </tbody>
            </table>
            """)


            #self.response.write('<p>%s</p>' % dbInfo.name)

    def post(self):
        gameName = self.request.get("gameName")
        gameDescription = self.request.get("gameDescription")
        gameCompany = self.request.get("company")
        gameConsole = self.request.get("console")
        gameGenre = self.request.get("genre")
        gameRecommend = self.request.get("recommend")
        #If gameRecommend is checked, mark yes. Otherwise, mark no.
        if gameRecommend == "Recommended":
            gameRecommend = "Yes"
        else:
            gameRecommend = "No"

        #Post the information into the datastore
        myGames = MyGames(
            name = gameName,
            description = gameDescription,
            company = gameCompany,
            console = gameConsole,
            genre = gameGenre,
            recommend = gameRecommend)
        myGames.put()
        self.redirect('/')

我在这里做错了吗?

【问题讨论】:

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


    【解决方案1】:

    这是因为数据存储区的Eventual Consistency。添加到数据存储区的项目可能需要一段时间才能对不直接使用其密钥的查询可见。 MyGames.query() 在你的情况下。

    【讨论】:

      猜你喜欢
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2016-04-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多