【发布时间】: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