3

我正在使用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'

主文件

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)

我究竟做错了什么?非常感谢任何帮助/建议!

4

3 回答 3

2

calculateYear如果您为您的BaseHandler班级提供功能(或其他地方,如果更适用),它是否符合您的标准?如您所料,您x没有被视为适当的response. 当您调用webapp2.RequestHandler处理程序时,它会调用与请求类型关联的方法(因此,在您的情况下,由于您正在发布表单,它将调用post(),如您所知)。当您实例化x和调用calculateYear时,您没有指定特定的方法(def get(self)def post(self)等),因此没有准备 a response(当我有机会时,我会稍微挖掘一下以确认情况确实如此 - 我可能是错误的:) )。

现在无法对此进行测试,但假设您需要调用calculateYear的不仅仅是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处理程序中调用它,例如:

class CaptureDetails(BaseHandler):
  def post(self):
    name = self.request.get("name").strip()
    age = self.request.get("age").strip()

    # Call the function and write the response
    self.calculateYear(name, age)
于 2012-12-14T20:00:54.133 回答
1

通过在您的方法中实例化Calculate自己,您CaptureDetails.post不会以与创建方法相同的方式创建它WSGIApplication,因此属性不可用。具体来说,您没有将 a 传递response给它,因此尝试引用它不起作用也就不足为奇了。

在这种情况下,我会将 的内容复制calculateYear到您的 post 方法中——您并没有真正通过创建实例然后调用它的方法来保存任何东西。如果calculateYear变得更复杂,并且您不希望重复,那么我将介绍一种可以由您的两个处理程序方法调用的新方法。(虽然这个类为什么存在,但从这个例子中并不清楚Calculate——它没有“get”方法,所以/age按照你所做的映射它是行不通的。)

于 2012-12-14T20:02:23.413 回答
0

尝试使用self.response.out.write而不是self.response.write.

于 2012-12-14T19:36:13.517 回答