0

更新 #7 :

最初の行を削除し、その場所に jinja2 リンクjinja2 install docsからコピーした 2 行目を配置しました。しかし、私はまだ以下のエラーが発生します。

# from google.appengine.ext.webapp import template
from jinja2 import Template

  File "/Users/brian/googleapps/YouPoll/views.py", line 271, in get
    self.response.out.write(template.render(path, template_values))
NameError: global name 'template' is not defined
INFO     2012-07-08 22:43:53,053 dev_appserver.py:2952] "GET /?ID=testBSchott HTTP/1.1" 500 

この appengine は、テンプレート ファイル内の非 Django コードで少し前まで機能していました。

さらに、gae Interactive Console で次のコードを実行すると、django ではなく jinja2 に固有であると思われる {%- if ... -%} コードが含まれていても機能します。

from jinja2 import Template
template = Template('{%- if True -%}Hello {{ name }}!{% endif %}')
print template.render(name='John Doe')

更新 #6 :

わかりました、変数テンプレートが定義されていることがわかりました。以下です。

jinja_environment.filters['datetimeformat'] = datetimeformat

class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app)

    def render_template(
        self,
        filename,
        template_values,
        **template_args
        ):
        template = jinja_environment.get_template(filename)
        self.response.out.write(template.render(template_values))

更新 #5:

これがviews.pyでの私のインポートです:

import jinja2
import os
import webapp2
import logging
import datetime
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images
# from jinja2 import Environment, PackageLoader

from models import QA, PQ

TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
jinja_environment = \
    jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR))

私の template_values の使用は、私が投稿したいくつかのコードの下にあります。インポートのうち 2 つは、webapp2 ではなく、webapp に言及しています。しかし、最初のものを webapp2 に変更すると、このエラーが発生します。

  File "/Users/brian/googleapps/YouPoll/views.py", line 9, in <module>
    from google.appengine.ext.webapp2 import template
ImportError: No module named webapp2
INFO     2012-07-08 18:44:18,972 dev_appserver.py:2952] "GET /?ID=testBSchott HTTP/1.1" 500 

そして、その行を削除すると、このエラーが発生します。

  File "/Users/brian/googleapps/YouPoll/views.py", line 272, in get
    self.response.out.write(template.render(path, template_values))
NameError: global name 'template' is not defined
INFO     2012-07-08 18:30:52,733 dev_appserver.py:2952] "GET /?ID=testBSchott HTTP/1.1" 500

私にできることはありますか?必要な情報を提供しましたか?

更新 #3 : これら 5 つの jinja2 シーケンス/フレーズを削除すると、html フォームは正常に機能します。しかし、これら 5 つのシーケンス/フレーズには構文エラーはありません。彼らは以前はうまく機能していました。そして、最初の更新で、django を含む Traceback を以下に追加しましたが、django は使用していませんが、jinja のみです。では、他にどのようなエラーが発生していますか?

上記のシーケンス/フレーズを削除する前に、HTML ページの下部にも含めました。

1

{% for choice in p.choices -%}
{% if not loop.first %}
,{% endif %}
 {% if loop.last %}
   &nbsp;or&nbsp;
 {% endif %}
 {{ choice }} 
{%- endfor %}.

2 {% for choice in p.choices %} {% if not loop.first %} ,{% endif %} {% if loop.last %} or {% endif %} {{ choice }} {% endfor %} .

3

{{ q.answers|sum() }}

4

q.date|datetimeformat('%d-%m-%Y')

5

q.modified|datetimeformat('%d-%m-%Y')

アップデート #3 の終了

エラーの前に発生するこのフレーズは、正常に機能します。

   {% if p.purpose %}
   </p>
  <p> The purpose of this questionnaire is {{ p.purpose }}.
  </p>
   {% endif %} 

しかし、この次のフレーズは (明らかに) エラーをスローします。

{% for choice in p.choices -%}
{% if not loop.first %}
,{% endif %}
 {% if loop.last %}
   &nbsp;or&nbsp;
 {% endif %}
 {{ choice }} 
{%- endfor %}. 

次はエラーです。

更新 #2 : 「誤った」「for」ステートメントを修正して、スペース コントロールの「-」を削除すると、システムは引き続き次のエラーに進みますが、「-」は実際にはエラーではありません。

ERROR    2012-07-06 21:46:07,107 webapp2.py:1553] 'for' statements should use the format 'for x in y': for choice in p.choices -
Traceback (most recent call last):

更新#1

  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/brian/googleapps/YouPoll/views.py", line 272, in get
    self.response.out.write(template.render(path, template_values))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 89, in render
    t = _load_internal_django(template_path, debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 163, in _load_internal_django
    template = django.template.loader.get_template(file_name)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader.py", line 160, in get_template
    template = get_template_from_string(template, origin, template_name)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader.py", line 168, in get_template_from_string
    return Template(source, origin, name)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 158, in __init__
    self.nodelist = compile_string(template_string, origin)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 186, in compile_string
    return parser.parse()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
    compiled_result = compile_func(self, token)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader_tags.py", line 195, in do_extends
    nodelist = parser.parse()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
    compiled_result = compile_func(self, token)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/loader_tags.py", line 173, in do_block
    nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/__init__.py", line 281, in parse
    compiled_result = compile_func(self, token)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/_internal/django/template/defaulttags.py", line 684, in do_for
    " 'for x in y': %s" % token.contents)

TemplateSyntaxError: 'for' statements should use the format 'for x in y': for choice in p.choices -
INFO     2012-07-06 21:46:07,117 dev_appserver.py:2952] "GET /?ID=testBSchott HTTP/1.1" 500 -

そして、テンプレートからそのフレーズを削除すると、これはエラーをスローします (他のいくつか (%ifs および {%fors ) をスキップした後)。

{% for q in qs|sort(attribute='seqnum') %}

次はエラーです。

ERROR    2012-07-06 21:48:38,177 webapp2.py:1553] Invalid filter: 'sort'
Traceback (most recent call last):
...
TemplateSyntaxError: Invalid filter: 'sort'
INFO     2012-07-06 21:44:03,051 dev_appserver.py:2952] "GET /?ID=testBSchott HTTP/1.1" 500 -

そして、テンプレートからそのフレーズを削除すると、これはエラーをスローします (他のいくつか (%ifs および {%fors ) をスキップした後)。

<td align="right"> {{ q.answers|sum() }} </td>

テンプレートからそのフレーズを削除すると、これもエラーになります。

ERROR    2012-07-06 21:36:07,650 webapp2.py:1553] Invalid filter: 'sum'
Traceback (most recent call last):
...
TemplateSyntaxError: Invalid filter: 'sum'

などなど。これは、何か他の問題があるというシグナルのように見えます。しかし、私には手がかりがありません。このテンプレートは少し前までは機能していましたが、機能しなくなったので、私の Python コードに何らかのエラーが原因であると思われますが、どこから調べればよいかわかりません。

ところで、私が jinja2 を使用しているとき、Traceback は Django について頻繁に言及していますが、それは無関係だと思います。

以下は、テンプレートをフィードするために使用する python コードです。私が行ったデバッグの一部を示すために、ログ情報を残しました。

qs = db.Query(QA)
qs.ancestor(person)
qs.filter('deleteRequested =', False)
qs.run()
for item in qs:
    logging.info("2 seqnum %d" % item.seqnum)
for item in person.choices:
    logging.info("2 choice %s" % item )
logging.info("2 title %s" % person.title  )
logging.info("2 person.key()%s" % person.key())

# template_values = {'ID_id':person.key(), 
template_values = { 'p': person,
    'qs': qs
    }
path = os.path.join(TEMPLATE_DIR, 'table.html')
logging.info("Here After Revise" )
self.response.out.write(template.render(path, template_values))

更新 #4 :

以下は、失敗している私のhtmlページのリストです。

{% extends "base.html" %}
{% block content %}
<center>
    <h1>Make your preferences for <span class="emph">{{p.ID|escape}}</span> known here</h1>
   {% if p.title %}
  </center>
  <p><h2> The title of this questionnaire is: {{ p.title }}.</h2> </p>
  </center>
   {% endif %} 
  <br />

   <div id="inputdata">
   {% if p.purpose %}
  <p> The purpose of this questionnaire is: {{ p.purpose }}.
  </p>
   {% endif %} 
  <p>This is a participative preferences survey: you not only answer questions, you supply questions for all to answer.
  </p>
  <ol>
<li>
  To answer existing questions, click on the "Radio" button in the corresponding rows of the table: 
{% for choice in p.choices -%}
{% if not loop.first %}
,{% endif %}
 {% if loop.last %}
   &nbsp;or&nbsp;
 {% endif %}
 {{ choice }} 
{%- endfor %}.  
</li>
<li>
  After answering questions, at the bottom of the page click the "Submit" button to register you answers.
</li>
</ol>
  </div>
<br />
   <div id="inputdata">
  <p>To supply a new question or statement, click <a href="/add_question?ID={{ p.ID }}"><span class="emph">here</span></a>. At the present time there is no automated mechanism for editing or erasing an existing question.
  </p>
<br />
  <p>The question numbers (No's) are used to arrange the questions.  You place your question in the list with the number you choose.
  </p>
  </div>
   {% if not p.moreinfo == 'None' %}
   <p> {{ p.moreinfo }} 
   </p>
   {% endif %} 
<form action="table" method="post" enctype="multipart/form-data">
       <input type="hidden" name="ID" value="{{p.ID|escape}}"></input><br/>
  <table border="1" cellpadding="2" 
      cellspacing="0" class="sortable" id="unique-id">
 <thead> <tr> 
<td>No</td>
<td>Question</td>
<td> Total </td>
{% for choice in p.choices %}
<td bgcolor="#fef886"> {{ choice }} </td>
{% endfor %}
<td> Created </td>
<td> Last answered </td>
</tr></thead>

<tbody>
    {% if qs %}
{% for q in qs|sort(attribute='seqnum') %}
    <tr>
    <td align="right"> {{ q.seqnum }} </td>
    <td> {{ q.question }} </td>
    <td align="right"> {{ q.answers|sum() }} </td>
    {# <td> {{ q.total }} </td> #}
        {% for answer in q.answers %}
    <td bgcolor="#fef886"> 
        <input type="radio" name="{{q.seqnum}}" value="{{loop.index}}"
        />
        {{ answer }} 
    </td>
        {% endfor %}
<td>{{ q.date|datetimeformat('%d-%m-%Y') }}</td>
<td>{{ q.modified|datetimeformat('%d-%m-%Y')}}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
  <br />
   <div id="inputdata">
       <p>To remember your own choices, print this page before ReLoading</p>
  </div>
<center>
  <input type="submit" 
   name="Submit" 
   value="Submit" /> 
  </center>
  </form>   

<script src="/static/javascript/sorttable.js" type="text/javascript"></script>

{% endblock content %}
4

1 に答える 1

1

Wooble は、 from を削除して にgoogle.appengine.ext.webapp import template置き換えるようにコメントすることで、この質問に実際に答えましfrom jinja2 import Templateたが、元の問題を引き起こした可能性のある別のエラーもありました。ワンポイントself.response.out.write(template.render(path, template_values))代わりに使いました。self.render_template('table.html', template_values )

重要な問題は、私が jinja2 を使用していると思っていたときに、gae が django を使用していたことであり、Wooble は、元の質問でさりげなく言及しただけの事実に基づいていました。

(だから、Woobleがこの答えの功績を認めてくれることを願っています。)

于 2012-07-09T11:07:55.487 に答える