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)
application:template-lesson
version: 1
runtime: python27
api_version: 1
threadsafe: True
handlers:
- url: /.*
script: templates.app
python template
<!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>
{% 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 %}