View-Controller Mapping

 HTML Template File index.html

<html>
  <body>
    <h2>Interest Calculator</h2>   
    <h3>Enter the principal and interest rate</h3>

    <form action="/on_calculate" method="get">
      Principal:<input type="text" name="principal" value={{principal}}>
      Rate(%):<input type="text" name="rate" value={{rate}}>
      <input type="submit" value="Calculate"></div>
    </form>

    Interest Earned: {{interest}}
 </body>
</html>

Controller Code: financial_controller.py


class MainPage(webapp.RequestHandler):
  def get(self):
    template_values={'principal':'1000','rate':'10'}
    # render the page using the template engine
    path = os.path.join(os.path.dirname(__file__),'index.html')
    self.response.out.write(template.render(path,template_values))

class CalcController(webapp.RequestHandler):
  def get(self):
    principalString=self.request.get('principal')
    rateString = self.request.get('rate')
    principal = int(principalString)
    rate = int(rateString)
    interest = principal*rate/100
    interestString = str(interest)
   # set up the template_values with the list of people returned.
    template_values= {'interest':interestString, 'principal':principalString,'rate':rateString}
    # render the page using the template engine
    path = os.path.join(os.path.dirname(__file__),'index.html')
    self.response.out.write(template.render(path,template_values))

application = webapp.WSGIApplication(
                  
                                     [('/', MainPage),
                
                                      ('/on_calculate, CalcController)],
                                     debug=True)

Application config file: app.yaml

    application: davesFinancial
    version: 1
    runtime: python
    api_version: 1
    handlers:
    - url: /.*
      script: financial_controller.py

Suppose you've uploaded your application to davesFinanical.appspot.com.

1. When the user enters davesFinancial.appspot.com in a browser, the server looks in app.yaml. What does that config file tell us. Draw a line in the code to the left showing any mappings defined.





2. Near the bottom of the controller code is a table mapping requests to controller classes. For the URL davesFinancial.appspot.com, what is the request? What class will handle it? Draw a line showing any mappings defined.




3. What does the 'get' method within the controller classes correspond to?




4. The controller class sets up key-value pairs in a template_values hash table. What do the keys correspond to in the HTML template? What do the values correspond to? Draw lines showing the mappings.



5. What happens when the user submits the form in index.html? Draw lines for all the mappings in the entire process.



What is self.request.get?

Why is the str method called in CalcController.

Recent site activity