1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import os import webapp2 form_html = """ <form> <h2>Add a Food</h2> <input type="text" name="food"> %s <button>Add</button> </form> """ hidden_html = """ <input type="hidden" name="food" value="%s"> """ shopping_list_html = """ <br> <br> <h2>Shopping List</h2> <ul> %s </ul> """ class Handler(webapp2.RequestHandler): def write( self , * a, * * kw): self .response.out.write( * a, * * kw) class MainPage(Handler): def get( self ): output = form_html hidden_html = "" items = self .request.get_all( "food" ) if items: output_items = "" for item in items: output_hidden + = hidden_html % item output_items + = item_html % item output_shopping = shopping_list_html % output_items output + = output_shopping output = output % output_hidden self .write(output) app = webapp2.WSGIApplication([( '/' , MainPage), ], debug = True ) |
1 2 3 4 5 6 7 8 9 | application:template-lesson version: 1 runtime: python27 api_version: 1 threadsafe: True handlers: - url: /.* script: templates.app |
python template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <! DOCTYPE html> < html > < head > < title >templates!</ title > </ head > < body style = "margin: 0" > < h1 style = "background-color: #ddd: color: #888; margin: 0, height: 50px" > Templates </ h1 > {% block content %} {% endblock %} </ body > </ html > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | {% extends "base.html" %} {% block content %} < form > < h2 >Add a Food</ h2 > < input type = "text" name = "food" > {% if items %} {% for item in items %} < input type = "hidden" name = "food" value = "{{item}}" > {% endfor %} < button >Add</ button > {% if items %} < br > < br > < h2 >Shopping List</ h2 > < ul > {% for item in items %} < li >{{ item | escape }}</ li > </ ul > {% endif %} </ form > {% endblock %} |