I have a form on a page and submit the details of the form on the click of the submit button and want to refresh the page after the object is added to the datastore. I read online that this could be because of the datastore's Eventual Consistency but can't seem to figure out a way to achieve the desired result(refreshing the page).
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)
It would be great if someone could have a look at the code and give me some input on how to resolve the issue.
Any help is really appreciated! Thanks!
Not sure if this is what you are looking for, but in general, if you want to refresh the page, you should do it using Javascript/JQuery on your page.
Have your endpoint send back a JSON response back to the '/profile'. The response should look something like:
{"success":"success"}
Or if, you need to send an error message:
{"error": "insert error message here"}
Your Javascript and/JQuery should then check if "error" is in the response. If it is throw an error message, otherwise, refresh the page.