2011-03-29 81 views
2

我無法意識到我做錯了什麼。我在GAE數據存儲中有一些條目。我有Jinja2進口。我想用Jinja2在頁面上顯示數據存儲條目。我創建了一個快捷函數來調用Jinja2渲染函數。它看起來像這樣:無法使用Jinja2從GAE數據存儲渲染數據

def render_template(response, template_name, vars=dict()): 
    template_dirs = [os.path.join(root(), globals['templates_root'])] 
    env = Environment(loader=FileSystemLoader(template_dirs)) 
    try: 
     template = env.get_template(template_name) 
    except TemplateNotFound: 
     raise TemplateNotFound(template_name) 
    content = template.render(vars) 
    response.response.out.write(content) 

因此,只有我要傳遞給這個函數就是一個模板文件名,並與如果存在變量的字典。我調用這個函數是這樣的:

class MainHandler(webapp.RequestHandler): 
    def get(self, *args, **kwargs): 
     q = db.GqlQuery("SELECT * FROM Person") 
     persons = q.fetch(20) 
     utils.render_template(self, 'persons.html', persons) 

模型Person看起來是這樣的,沒有什麼特別的有:

class Person(db.Model): 
    first_name = db.StringProperty() 
    last_name = db.StringProperty() 
    birth_date = db.DateProperty() 

當我嘗試通過persons字典render_template,它拋出一個錯誤:

TypeError: cannot convert dictionary update sequence element #0 to a sequence 

它不呈現。當我通過空{}作爲persons的說法,它呈現,但顯然沒有我的數據。 我做錯了什麼?我確信有一些我錯過的小東西,但我不知道究竟是什麼。謝謝!

回答

2

您正在向您的render_template函數傳遞一個實體列表,而不是傳遞字典。嘗試像utils.render_template(self, 'persons.html', {'persons': persons})

+0

絕對!我開始瘋狂了。謝謝! – 2011-03-29 15:39:33