我正在使用Nick Johnsons Webapp & Templates应用程序作为我想要做的事情的基础。当我尝试调用render_template时,我得到一个“AttributeError:'NoneType'对象没有属性'write'”。我知道这是因为当我将对象“Capture”实例化为X时,它没有响应属性。我到处寻找解决方案,但我找不到任何地方。
注意:还有其他方法可以做到这一点,但我需要它与我如何设置它一起工作!
回溯:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/userx/Documents/_FRESHCUTZ MEDIA/Google/GAE - Test web form 1 /testwebform1/main.py", line 41, in post
x.calculateYear(name, age)
File "/Users/userx/Documents/_FRESHCUTZ MEDIA/Google/GAE - Test web form 1 /testwebform1/main.py", line 49, in calculateYear
self.response.write(self.render_template('index.html', **template_args))
AttributeError: 'NoneType' object has no attribute 'write'
MAIN.PY
import os
import webapp2
from webapp2_extras import jinja2
class BaseHandler(webapp2.RequestHandler):
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_template(self, filename, **template_args):
self.response.write(self.jinja2.render_template(filename, **template_args))
class MainPage(BaseHandler):
def get(self):
template_args = {}
self.render_template('index.html', **template_args)
class CaptureDetails(BaseHandler):
def post(self):
name = self.request.get("name").strip()
age = self.request.get("age").strip()
x = Calculate()
x.calculateYear(name, age)
class Calculate(BaseHandler):
def calculateYear(self, name, age):
template_args = {"age": age}
self.render_template('name.html', **template_args)
app = webapp2.WSGIApplication([
('/', MainPage),
('/capture', CaptureDetails),
('/age', Calculate)
], debug=True)
我做错了什么?非常感谢任何帮助/建议!
答案 0 :(得分:2)
如果您将calculateYear
作为BaseHandler
课程的一部分(或其他地方,如果更适用),它是否符合您的标准?如您所知,您的x
未被视为正确response
。当您调用webapp2.RequestHandler
处理程序时,它会调用与请求类型相关联的方法(因此,在您的情况下,由于您要发布表单,因此您将调用post()
,如您所知。当您实例化x
并致电calculateYear
时,您没有指定特定方法(def get(self)
,def post(self)
等),因此没有准备response
1}}(当我有机会的时候,我会稍微挖一点来确认实际情况就是这样 - 我可能会弄错:))。
现在无法对此进行测试,但假设您需要从calculateYear
以上的处理程序调用{{1}},这会有效吗?在这里,您将在CaptureDetails
方法的上下文中引用self
,这将调用post
处理:
response
然后您可以从class BaseHandler(webapp2.RequestHandler):
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_template(self, filename, **template_args):
self.response.write(self.jinja2.render_template(filename, **template_args))
def calculateYear(self, name, age):
template_args = {"age": age}
self.render_template('name.html', **template_args)
处理程序调用,如:
CaptureDetails
答案 1 :(得分:1)
通过Calculate
方法自己实例化CaptureDetails.post
,您不会像WSGIApplication
那样创建它,因此属性不可用。具体来说,你没有将response
传递给它,因此试图引用它并不奇怪。
在这种情况下,我将calculateYear
的内容复制到你的post方法中 - 你不是真的通过创建实例然后在其上调用方法来保存任何东西。
如果calculateYear
变得更复杂,并且您不想复制,那么我将介绍一种可以由您的两种方法调用的新方法。 (虽然这个例子虽然存在Calculate
类,但它并没有真正清楚 - 它没有'get'方法,所以将它映射到/age
就像你所做的那样无效。 )
答案 2 :(得分:0)
尝试使用self.response.out.write
代替self.response.write
。