当我加载 html 页面时,数据库中的结果没有出现。我正在使用 python 和 jinja2。{{ initial_city }} 显示正确的结果,问题似乎出在 get_deals 或我使用 jinja2 的 html 文件上。我想显示与主页上选择的城市相同的结果(它是一个表单)。此外,我在数据库中有实体,这些实体在进入谷歌数据存储本地扩展时出现
get_deals 代码是:
def get(self):
def get_deals(choose_city, update=False):
key = str(choose_city)
all_deals = memcache.get(key)
if all_deals is None:
d = Deal.all()
all_deals = d.filter('city = ', choose_city)
all_deals = list(all_deals)
memcache.set(key, all_deals)
return all_deals
以下是我的脚本:
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.ext.db import stats
from models import Deal
import os
import webapp2
import jinja2
class Deal(db.Model):
title = db.TextProperty()
description = db.TextProperty()
city = db.TextProperty()
deal = Deal(title='Hello',
#description='Deal info here',
#city='New York')
deal.put()
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render())
class DealHandler(webapp2.RequestHandler):
def get(self):
def get_deals(choose_city, update=False):
key = str(choose_city)
all_deals = memcache.get(key)
if all_deals is None:
d = Deal.all()
all_deals = d.filter('city = ', choose_city)
all_deals = list(all_deals)
memcache.set(key, all_deals)
return all_deals
choose_city = self.request.get('c')
try:
initial_city = str(choose_city)
choose_city = initial_city
except ValueError:
initial_city = 0
choose_city = initial_city
template = jinja_environment.get_template('deal.html')
self.response.out.write(template.render(initial_city = initial_city, get_deals = get_deals))
app = webapp2.WSGIApplication([('/', MainPage),
('/deal', DealHandler),
('/content', ContentHandler)],
debug=True)
我没有显示结果的html页面是:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/twitter-bootstrap/twitter-bootstrap- v2/docs/assets/css/bootstrap.css">
<title>
Deallzz: Chosen City: {{ initial_city }}
</title>
</head>
<body>
All deals:
{% for deal in deals %}
<li>{{ get_deals }}</li>
{% endfor %}
</body>
</html>