我正在学习web应用程序开发,并尝试分离模块,只是为了使我的程序更明显地面向对象,并且易于导航/理解。在
我在主.py工作正常:
import jinja2
import main_page # <<<- This import of my module works
import os
import webapp2
from string import letters
# loads templates to make our life easier
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
######## Main App Function ########
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
我在main中的第二次导入_页面.py不起作用:
^{pr2}$我在终端上得到了这个错误:
File "/Users/James/Developer/Google-App-Engine/Cookies/main_page.py", line 6, in class MainPage(BaseHandler): NameError: name 'BaseHandler' is not defined
我尝试过更改文件名和类名,因为它们与其他模块混淆了。这些文件都在同一个文件夹中。在
This is the **main_handler.py**:
import webapp2
import string
import jinja2
import hashlib
import hmac
SECRET = "test secret"
# global hash functions
def hash_str(s):
return hmac.new(SECRET, s).hexdigest()
def make_secure_val(s):
return "%s|%s" % (s, hash_str(s))
def check_secure_val(h):
val = h.split('|')[0]
if h == make_secure_val(val):
return val
# this is a template with convenience methods
class BaseHandler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
导入“main_handler”时,可以这样使用:
不是这样的:
^{pr2}$相关问题 更多 >
编程相关推荐