google-app-enginegoogle-cloud-platformwebapp2

appengine/Django submit form using classes


I would like to create a registration from and have tried to follow the official documentation (https://cloud.google.com/appengine/docs/standard/python/getting-started/python-standard-env).

Right now, all i would like to do is display the form data to another route, but i get the following error:

  File "/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/vicktree/Desktop/noah/web/noahs-app/handlers/noah_handler.py", line 329, in dispatch
    super(NoahSiteHandler, self).dispatch()
  File "/home/vicktree/Desktop/noah/web/noahs-app/handlers/noah_handler.py", line 130, in dispatch
    webapp2.RequestHandler.dispatch(self)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/vicktree/Desktop/noah/web/noahs-app/handlers/site_handlers.py", line 1071, in post
    user_name = self.request.form['user_name']
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/lib/webob-1.1.1/webob/request.py", line 1238, in __getattr__
    raise AttributeError(attr)
AttributeError: form

My routes are the following:

   #Sign-in registration
    Route(r'/signin/registration', 'handlers.site_handlers.UserRegistration', name='registration'),

    #Sign-in Noah registration submit
    Route(r'/signin/submitted', 'handlers.site_handlers.UserRegistrationSubmit', name='registration_submit'),

My Class Logic is below:

class UserRegistrationSubmit(SiteHandler):

    template_filename = 'registration_submit.html'

    def post(self):
        user_name = self.request.form['user_name']
        password = self.request.form['password']
        self.render_template(values={'user_name':user_name, 'password':password})

class UserRegistration(SiteHandler):

    # email = request.form['email']

    template_filename = 'registration.html'

    def get(self):
        self.render_template()

My HTML template is below:

   /registration.html
    <div class="alpha-content grid-line">
    <head>
        <title>Submit a form</title>
       <link rel="stylesheet" type="text/css" href="/static/style.css">
      </head>
      <body>
        <div id="container">
          <div class="pagetitle">
            <h1>Submit a form</h1>
          </div>
          <div id="main">
            <form method="post" action="{{ url_for('registration_submit') }}">
              <label for="user_name">Username:</label>
              <input type="text" name="name"><br />
              <label for="password">Password:</label>
              <input type="password" name="password"><br />
              <input type="submit">
            </form>
          </div>
        </div>


        <div class="veneer"></div>
    </div>

   /registration_submit.html
    <html>
     <head>
       <title>Submitted form</title>
     </head>
     <body>
       <div id="container">
         <div class="pagetitle">
           <h1>Form submitted</h1>
         </div>
         <div id="main">
           <p>Thanks for your submission!</p>
           <p>Here's a review of the information that you sent:</p>
           <p>
              <strong>Name</strong>: {{user_name}} <br>
              <strong>Password</strong>: {{password}} <br>
           </p>
         </div>
       </div>
     </body>
    </html>

Solution

  • I suspect because you define the name attribute as "name" in your form:

    <input type="text" name="name"><br />
    

    Then try to access it as user_name in the logic:

    user_name = self.request.form['user_name']
    

    You therefor get the AttributeError

    These both need to match, so for clarity you could do something like:

    <input type="text" name="user_name"><br />
    
    user_name = self.request.form['user_name']