當我加載html頁面時,數據庫中的結果沒有出現。我正在使用python和jinja2。 {{initial_city}}顯示正確的結果,問題似乎與get_deals或我已經使用jinja2的html文件有關。我想顯示的結果等於主頁上選擇的城市(它是一種形式)。另外,我在數據庫中的實體,去到谷歌的數據存儲本地擴展時出現使用jinja2和python,頁面不顯示數據存儲查詢的結果。幫助查找問題?
的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>
您在模板中使用了'deals',但是傳遞了'get_deals'。嘗試在'response.out.write'中用'self.response.out.write(template.render(initial_city = initial_city,deals = get_deals(choose_city))來更改行') –