我的main.py文件中有一个json处理程序,我试图让这个处理程序与jinja2模板通信。处理程序如下:
class JSONHandler(Handler):
def get(self):
content = get_content(True)
self.response.headers['Content-type'] = 'application/json'
tshirt_json = []
for t in content:
tshirt_json.append({"id": t.unq_id, "product_name": t.product_name, "price": t.price, "qty": t.qty})
self.write(json.dumps(tshirt_json))
为简单起见,我的模板将简要列出如下:
{% extends 'base.html' %}
{% block content %}
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<div><center><h1>Gigi's Catering Menu</h1></center></div>
<form method = "post">
{% for i in range(150) %}
<table style="width:100%">
<tr>
<th>Large Gourmet Meat & Cheese Tray</th>
<th colspan="2"><p>Fresh Roast Sirloin Beef, Baked Ham, and Roasted Turkey with Swiss and American Cheese<br><i>Serves 20-25 people</i></p>
</th>
</tr>
<tr>
<td></td>
<td>Selection</td>
<td>Quantity</td>
</tr>
<tr>
<td>Price would be $54.99 Per Tray</td>
<input type="hidden" name="unq_id" value=" <td>Brownie</td>
<input type="hidden" name="unq_id" value="140">
<input type="hidden" name="product_name" value="Lunch Box: Dessert - Brownie">
<td><input type="checkbox" name="chkbox" value="True"></td>
<td><input type="text" name="qty" id="q01" value="{{qty}}"><div class="error">{{ error }}</div></td>
</tr>
<tr>
<td>Cookie</td>
<input type="hidden" name="unq_id" value="141">
<input type="hidden" name="product_name" value="Lunch Box: Dessert - Cookie">
<td><input type="checkbox" name="chkbox" value="True"></td>
<td><input type="text" name="qty" id="q01" value="{{qty}}"><div class="error">{{ error }}</div></td>
</tr>
</table>
<br>
<hr style="height:0px">
<br>
<label>
<div>Additional Comments and/or Special Requests</div>
<textarea name="body">{{body}}</textarea>
</label>
<div class="error">{{ error }}</div>
<input type="button" value="submit">
<input type="reset">
</form>
{% if chkbox is True %}
{% tmp_order = (i.unq_id, i.product_name, i.price, i.qty) %}
{% endif %}
{% endfor %}
{% endblock %}
">
<input type="hidden" name="product_name" value="Large Gourmet Meat & Cheese Tray">
<input type="hidden" name="price" value="{{54.99}}">
<td><input type="checkbox" name="chkbox" value="True"></td>
<td><input type="text" name="qty" id="q01" value="{{qty}}"><div class="error">{{ error }}</div></td>
</tr>
</table>
<br>
这是jinja2模板的尾端:
INSERT INTO ... SELECT
我想要完成的是用户将在模板上进行选择并单击提交按钮。其中的选择将使用上述json函数编译成字典,并能够对该函数进行验证。考虑到我正在处理我仍在努力的事情,我将非常感谢社区的帮助。我提前感谢社区。 p>