2012-10-04 16 views
0

當我加載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> 
+0

您在模板中使用了'deals',但是傳遞了'get_deals'。嘗試在'response.out.write'中用'self.response.out.write(template.render(initial_city = initial_city,deals = get_deals(choose_city))來更改行') –

回答

2
  1. 在您的代碼中,您不會調用get_deals函數。我建議你採取get_deals()get()並調用它像

    self.response.out.write(template.render(initial_city = initial_city, get_deals =self.get_deals(choose_city))) 
    

    ,因爲它是一個不錯的設計。

    編輯
    變化self.get_deals()爲「get_deals()`如果你不打算這樣做。

  2. 您在您的html中使用deals,但在從服務器調用時未指定其值。 也許你想要做的是

    {% for deal in get_deals %}  
        {{ deal }} 
    {% endfor %} 
    
+0

我編輯了Python代碼,現在我得到錯誤「AttributeError:'DealHandler的對象沒有屬性'get_deals'」 – Brett

+0

我編輯了Python代碼,現在我得到錯誤「AttributeError:'DealHandler'對象沒有屬性'get_deals '「 此外,對於html文件,我可以簡單地{{get_deals}}來顯示結果,或者是{%? %}要求? – Brett

+0

請參閱編輯。你需要使'get_deals()'類函數像我說的那樣調用它。 Ppl通常以這種方式模塊化他們的代碼,我甚至認爲你也是這樣做的。 – Emil

0

你實際上並沒有調用get_deals函數。

+0

您能否進一步解釋?我應該調用all_deals嗎? – Brett

0

self.response.out.write(template.render(initial_city = initial_city, get_deals = get_deals)) 

什麼周華健建議應該工作以及你可以嘗試將

get_deals = get_deals(choose_city) 

。 希望有所幫助!

+0

這兩個trys我都[]作爲輸出? – Brett

+0

這可能是一個stackoverflow的神器,但我認爲你的縮進不太正確。 choose_city = self.request.get('c'),它下面的代碼應該符合您對get_deals函數的定義。 – dafeda