GAE:如何在 GAE 上提交表单后刷新页面?

GAE : How to refresh page after submitting form on GAE?

我在页面上有一个表单,单击提交按钮提交表单的详细信息,并希望在将对象添加到数据存储后刷新页面。我在网上看到这可能是因为数据存储的最终一致性,但似乎无法找到实现预期结果(刷新页面)的方法。

class AddUpdateProfile(webapp2.RequestHandler):
    def post(self):
        #This will be used to add/update profile in a datastore. Will be called when the user clicks on submit button on the Profile Page
        template = JINJA_ENVIRONMENT.get_template('profile.html')
        error = None
        name = self.request.get('name')
        designation = self.request.get('designation')
        salary = self.request.get('salary')
        currency = self.request.get('currency')
        logging.info("Name = "+name)
        logging.info("Designation = "+designation)
        logging.info("Salary = "+salary)
        logging.info("Currency = "+currency)

        profile = UserProfile(parent=userProfile_key(users.get_current_user().email()))
        profile.name = name
        profile.designation = designation
        profile.salary = int(salary)
        profile.currency = currency
        profile.email = str(users.get_current_user().email())
        profile.put()

        #Go back to main page. TODO : Change this to update 
        self.redirect('/profile')

class Profile(webapp2.RequestHandler):

    def get(self):
        logging.info("Inside Profile Page")
        user = users.get_current_user()

        if user:
            profileInfo = getProfileInformation(user.email())
            logging.info("Found a user inside Profile Page")
            url = users.create_logout_url(self.request.uri)
            if profileInfo is None or not profileInfo:
                logging.info("Email = "+user.email())
                logging.info("Profile Info not found")
                template_values = {
                'user': user.nickname(),
                'url': url
                }
            else:
                logging.info("Profile Info found")
                template_values = {
                'user': user.nickname(),
                'url': url,
                'name' : profileInfo[0].name,
                'designation' : profileInfo[0].designation,
                'salary' : profileInfo[0].salary,
                'currency' : profileInfo[0].currency
                }


            template_values = template_values
            template = JINJA_ENVIRONMENT.get_template('profile.html')
            self.response.write(template.render(template_values))
        else:
            logging.info("User not found. Loading Landing page")
            template_values = {
                'url' : users.create_login_url(self.request.uri)
            }
            template = JINJA_ENVIRONMENT.get_template('landing.html')
            self.response.write(template.render(template_values))


class MainPage(webapp2.RequestHandler):

    def get(self):
        logging.info("Inside MainPage")
        user = users.get_current_user()
        if user:
            logging.info("Found a user inside MainPage")
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'SIGN OUT'
            template_values = {
            'user': user.nickname(),
            'url': url,
            'userPage' : "no",
            'url_linktext': url_linktext,
            }
            template = JINJA_ENVIRONMENT.get_template('index.html')
            self.response.write(template.render(template_values))
        else:
            logging.info("User not found. Loading Landing page")
            template_values = {
                'url' : users.create_login_url(self.request.uri)
            }
            template = JINJA_ENVIRONMENT.get_template('landing.html')
            self.response.write(template.render(template_values))

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/profile', Profile),
    ('/addProfile', AddUpdateProfile)
], debug=True)

如果有人可以查看代码并就如何解决问题向我提供一些意见,那就太好了。

非常感谢任何帮助! 谢谢!

不确定这是否是您要查找的内容,但一般来说,如果您想刷新页面,您应该在页面上使用 Javascript/JQuery 来完成。

让您的终端向“/profile”发回 JSON 响应。响应应类似于:

{"success":"success"}

或者,如果您需要发送错误消息:

{"error": "insert error message here"}

您的 Javascript and/JQuery 应该检查 "error" 是否在响应中。如果抛出错误信息,否则,刷新页面。

How to reload a page using Javascript?