Google App Engine Python:重定向到同一页面以重新加载表格

时间:2015-10-10 20:36:16

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

我有一个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('/')

我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

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