2

私は自分が間違っていることを認識できません。GAE データストアにいくつかのエントリがあります。Jinja2 をインポートしました。Jinja2 を使用して、ページにデータストア エントリを表示したいと考えています。Jinja2 render 関数を呼び出すショートカット関数を作成しました。次のようになります。

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とレンダリングされますが、明らかにデータがありません。私は何を間違っていますか?見逃した小さなものがあると確信していますが、正確にはわかりません。ありがとう!

4

1 に答える 1

2

render_template辞書を渡す代わりに、エンティティのリストを関数に渡しています。次のようなものを試してくださいutils.render_template(self, 'persons.html', {'persons': persons})

于 2011-03-29T15:35:30.300 に答える