NameError:未定义全局名称“TemplateNotFound”

时间:2014-03-02 22:55:10

标签: python google-app-engine python-2.7 jinja2 nameerror

我正在从融合表中收集一些数据到我的python代码中,并从那里尝试将数据可视化到地图上。为此,我使用的是Jinja2。但是我收到了这个错误,我只是不明白为什么。这是我的代码

import webapp2
from webapp2_extras import jinja2
from webapp2_extras import json
import logging
import httplib2
from apiclient.discovery import build
import urllib
import numpy
from django.utils import simplejson
from itertools import chain
from collections import Counter
import math

# This API key is provided by google as described in the tutorial
API_KEY = '************************'

# This is the table id for the fusion table
TABLE_ID = '*************************'  
# I am specifying the right keys here

service = build('fusiontables', 'v1', developerKey=API_KEY)

class BaseHandler(webapp2.RequestHandler):
    @webapp2.cached_property
    def jinja2(self):
        # Returns a Jinja2 renderer cached in the app registry.
        return jinja2.get_jinja2(app=self.app)

    # lets jinja render our response
    def render_response(self, _template, context):
        values = {'url_for': self.uri_for}
        # logging.info(context)
        values.update(context)
        self.response.headers['Content-Type'] = 'text/html'

        # Renders a template and writes the result to the response.
        try: 
            rv = self.jinja2.render_template(_template, **values)
            self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
            self.response.write(rv)
        except TemplateNotFound:
            self.abort(404)

class MainHandler(BaseHandler):

    def get(self):
        """default landing page"""
        logging.info("in get************************************************************************")
        data = self.get_all_data()
        columns = data['columns']
        rows = data['rows']
        print columns
        query = "SELECT USState, NOFU2008 FROM " + TABLE_ID 
        data2008 = service.query().sql(sql=query).execute()
        print data2008['columns']
        query = "SELECT USState, NOFU2009 FROM " + TABLE_ID 
        data2009 = service.query().sql(sql=query).execute()
        # print data2009
        query = "SELECT USState, NOFU2010 FROM " + TABLE_ID 
        data2010 = service.query().sql(sql=query).execute()

        variables = {"data2008": data2008 , "data2009": data2009, "data2010": data2010}
        print variables["data2009"]
        self.render_response('index.html', variables)

    def get_all_data(self):
        """ collect data from the server. """

        # open the data stored in a file called "data.json"
        try:
            fp = open("data/data.json")
            response = json.load(fp)
        # but if that file does not exist, download the data from fusiontables
        except IOError:
            service = build('fusiontables', 'v1', developerKey=API_KEY)
            query = "SELECT * FROM " + TABLE_ID
            response = service.query().sql(sql=query).execute()
            fp = open("data.json", "r")
            #json.dump(response, fp)
            return response

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

以下是我在google appengine启动器控制台中看到的内容。

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\Yash\New folder\data\Assignments\jmankoff-byte3\main.py", line 63, in get
    self.render_response('index.html', variables)
  File "C:\Users\Yash\New folder\data\Assignments\jmankoff-byte3\main.py", line 40, in render_response
    except TemplateNotFound:
NameError: global name 'TemplateNotFound' is not defined
INFO     2014-03-02 17:49:26,928 module.py:612] default: "GET / HTTP/1.1" 500 1826

1 个答案:

答案 0 :(得分:1)

您需要从jinja2 module

导入该例外
from jinja2 import TemplateNotFound

或使用您模块中已有的jinja2全局的引用:

except jinja2.TemplateNotFound: