【问题标题】:Why am I getting this syntax error in my python-jinja2 app为什么我的 python-jinja2 应用程序中出现此语法错误
【发布时间】:2014-01-17 04:36:46
【问题描述】:

我是 python 和谷歌应用引擎的新手。我正在尝试创建这个从雅虎管道获取提要并使用 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 -

谢谢。

【问题讨论】:

    标签: python google-app-engine jinja2


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      有两个语法错误:

      1. 第 37 行末尾缺少 ]
      2. post 函数在 MainHandler 中的缩进不正确。 Python 语法对缩进敏感,因此保持一致非常重要。

      查找语法错误时,请尽可能仔细阅读错误消息。当你刚开始时,很多可能没有多大意义,但尽可能多地尝试和锻炼。在这种情况下,对Line 38 的引用是一个很好的提示。仔细阅读该行,如果它看起来不错,就开始往上面看。

      使用支持语法高亮的编辑器也非常方便,这将使这些类型的语法错误立即显而易见。

      【讨论】:

        猜你喜欢
        • 2011-03-24
        • 2021-08-11
        • 2013-09-15
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        相关资源
        最近更新 更多