0

私はページにフォームを持ち、送信ボタンのクリックでフォームの詳細を提出し、オブジェクトがデータストアに追加された後にページを更新したいと考えています。私はこれがデータストアの最終的な一貫性のためにあるかもしれないが、望ましい結果(ページをリフレッシュする)を達成する方法を理解しているようには見えないことをオンラインで読んだ。GAE: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) 

誰かがコードを見て、私に問題を解決する方法についていくつかの入力を与えることができればそれは素晴らしいことです。

本当にありがとうございます! ありがとう!

+1

あなたは問題を記述していません。 *最終的な一貫性のために*は何ですか? –

+0

私は答えを出すことができると思うが、ダニエルが指摘しているように、問題をより正確に記述し、質問を短くする必要がある - 人々がすべてのコード(給与、通貨など)を見る必要はない。できるだけ短いコードを作成してください。編集が終わったら私は答えを提供することができます。 –

答えて

1

あなたが探しているものかどうかは不明ですが、一般的にページを更新したい場合は、ページのJavascript/JQueryを使用してください。

エンドポイントにJSON応答を '/ profile'に戻してもらうようにしてください。応答は次のようになります。

{"success":"success"} 

をそれとも、あなたはエラーメッセージを送信する必要がある場合:「エラー」が対応している場合

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

あなたのJavascriptと/ jQueryのは、チェックする必要があります。エラーメッセージがスローされた場合は、ページをリフレッシュします。

How to reload a page using Javascript?

関連する問題