为什么我在python-jinja2应用程序中出现此语法错误

时间:2014-01-17 04:36:47

标签: python google-app-engine jinja2

我是python和谷歌应用引擎的新手。我正在尝试创建这个从雅虎管道获取Feed并使用jinja2模板显示它的应用程序。但是我收到语法错误,我不理解它背后的原因。

导入webapp2 来自webapp2_extras导入jinja2 导入日志记录

import feedparser  
import urllib

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

    def render_response(self, _template, **context):
        rv = self.jinja2.render_template(_template, **context)
        self.response.write(rv)

class MainHandler(BaseHandler):
    def get(self):
        feed = feedparser.parse("http://pipes.yahoo.com/pipes/pipe.run?_id=1nWYbWm82xGjQylL00qv4w&_render=rss&textinput1=dogs" )

        feed = [{"link": item.link, "title":item.title, "description" : item.description} for item in feed["items"]
        context = {"feed" : feed, "search" : "dogs"}
        self.render_response('index.html', **context)

        def post(self):

            terms = self.request.get('search_term')

            terms = urllib.quote(terms)
            feed = feedparser.parse("http://pipes.yahoo.com/pipes/pipe.run?_id=1nWYbWm82xGjQylL00qv4w&_render=rss&textinput1=" + terms )

            feed = [{"link": item.link, "title":item.title, "description" : item.description} for item in feed["items"]]

            context = {"feed": feed, "search": terms}

            self.render_response('index.html', **context)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

这是index.html文件

<!DOCTYPE html>
<html>
<head>
<title>Byte 1 Tutoral</title>
</head>
<body>
<h1>Data Pipeline Project Byte 1 Example</h1>
<form action="search" method="POST">
  Search Term: <input name="search_term" value={{search}}><br>
  <input type="submit" value="Enter Search Term">
</form>
{% if search: %}
<p>Searching for {{search}}</p>
{% endif %}


<h2>Feed Contents</h2>
{% for item in feed %}
<a href="{{ item.link }}">{{ item.title }}</a><br>
{{item.description|safe}}
<br>
{% endfor %}


</body>
</html>

这是我得到的错误。

Traceback (most recent call last):

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in Handle

    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 298, in _LoadHandler

    handler, path, err = LoadObject(self._handler)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 84, in LoadObject

    obj = __import__(path[0])

  File "C:\googleapps\ykelkar-byte1\main.py", line 38

    context = {"feed" : feed, "search" : "dogs"}

          ^

SyntaxError: invalid syntax

INFO     2014-01-16 23:15:25,845 module.py:612] default: "GET / HTTP/1.1" 500 -

感谢。

2 个答案:

答案 0 :(得分:0)

在第37行的末尾应该还有一个]:

feed = [{"link": item.link, "title":item.title, "description" : item.description} for item in feed["items"]]

答案 1 :(得分:0)

有两种语法错误:

  1. 从第37行末尾遗漏]
  2. postMainHandler功能的缩进不正确。 Python语法是缩进敏感的,因此保持它一致非常重要。
  3. 查找语法错误时,请尽可能仔细阅读错误消息。当你开始时,很多可能没有多大意义,但尽可能多地尝试。在这种情况下,Line 38的引用是一个很好的提示。仔细阅读该行,如果看起来不错,请从上面开始查看。

    使用支持语法突出显示的编辑器也非常方便,这会使这些语法错误立即变得明显。