diff --git a/appengine/standard/hello_world/app.yaml b/appengine/standard/hello_world/app.yaml index f041d384c05..ef5a5697bac 100644 --- a/appengine/standard/hello_world/app.yaml +++ b/appengine/standard/hello_world/app.yaml @@ -5,3 +5,7 @@ threadsafe: true handlers: - url: /.* script: main.app + +libraries: +- name: jinja2 + version: latest diff --git a/appengine/standard/hello_world/main.py b/appengine/standard/hello_world/main.py index c26ad7a80f6..811ea565f2e 100644 --- a/appengine/standard/hello_world/main.py +++ b/appengine/standard/hello_world/main.py @@ -1,26 +1,29 @@ -# Copyright 2016 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - import webapp2 +import cgi +import jinja2 +import os +# type hidden: include values in our query that the user can't see or interact with. This is different from type=password. Note that multiple query items can have +# the same name, so if you have a hidden input with name food and a text input with name food: ?food=val1&food=val2 is possible -class MainPage(webapp2.RequestHandler): - def get(self): - self.response.headers['Content-Type'] = 'text/plain' - self.response.write('Hello, World!') +template_dir = os.path.join(os.path.dirname(__file__), 'templates') # os.path.dirname(__file__) is location of this file +jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True) # when we render templates, jinja will look for them in this directory + +class Handler(webapp2.RequestHandler): + def write(self, *a, **kw): + self.response.out.write(*a, **kw) + def render_str(self, template, **params): + t = jinja_env.get_template(template) # create a jinja template from the file argument template + return t.render(params) # see example + + def render(self, template, **kw): + self.write(self.render_str(template, **kw)) # send to browser + + +class MainPage(Handler): + def get(self): + items = self.request.get_all('food') # get all params with name food + self.render("shopping_list.html", items=items) # again, we are dealing with user input. Consider data validation and HTML escaping -app = webapp2.WSGIApplication([ - ('/', MainPage), -], debug=True) +app = webapp2.WSGIApplication([('/', MainPage)], debug=True) diff --git a/appengine/standard/hello_world/templates/base.html b/appengine/standard/hello_world/templates/base.html new file mode 100644 index 00000000000..638c10feaf8 --- /dev/null +++ b/appengine/standard/hello_world/templates/base.html @@ -0,0 +1,11 @@ + + +
+