Question

In this code below I am using webapp2 to learn web programming from udacity.com and this is one of the exercises. What I am trying to achieve here is to render the form where user can enter some value and after hitting submit button user should see the their encrypted rot13 data in the form itself.

I am facing the issue whenever I hit submit button I my form turns out to be blank and user data is lost, I used pdb to to the self.request.get("content") in the post method and it turned out to be empty.

  import webapp2
  import cgi

  def escape_html(s):
    return cgi.escape(s, quote = True)


  form = """
    <!DOCTYPE html>
      <html>
      <head>
      <title>Unit 2 Rot 13</title>
      </head>
      <body>
      <h2>Enter some text to ROT13:</h2>
      <form method="post">
      <textarea name="text" style="height: 100px; width: 400px; ">
      %(content)s
      </textarea>
      <br>
      <input type="submit">
      </form>
      </body>
      </html>
      """

 class MainPage(webapp2.RequestHandler):
      def write_form(self, content = ""):
          self.response.out.write(form %{"content": escape_html(content)
                                   })
      def get(self):
          self.write_form()

      def post(self):
          content = self.request.get("content").encode("rot13")
          self.write_form(content)

  app = webapp2.WSGIApplication([('/', MainPage)],debug=True)
Was it helpful?

Solution

The field in your html firm is called text, not content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top