代码之家  ›  专栏  ›  技术社区  ›  Julius F

实体存在,返回空模板

  •  0
  • Julius F  · 技术社区  · 14 年前

    我得到了以下非常基本的模板:

    <html>
    <head>
    </head>
    <body>
    
    <div>
    <!-- Using "for" to iterate through potential pages would prevent getting empty strings even if only one page is returned because the "page" is not equal the query, it is a subcomponent of the query -->
    <div>{{ page.name }}</div>
    <div>{{ page.leftText }}</div>
    <div>{{ page.imageURL }}</div>
    <div>{{ page.rightText }}</div>
    </div>
    
    </body>
    </html>
    

    最基本的模型是:

    class Page(db.Model):
     name = db.StringProperty(required=True)
     leftText = db.TextProperty()
     rightText = db.TextProperty()
     imageURL = db.LinkProperty()
    

    最基本的处理程序:

    class BaseRequestHandler(webapp.RequestHandler):
        #######
    class PageContentLoadRequestHandler(BaseRequestHandler):
    
     def renderPage(self, values):
      directory = os.path.dirname(__file__)
      path = os.path.join(directory, 'templates', 'simple_page.html')
      return template.render(path, values, True)
    
     def get(self):
      page = db.get('aghwc21vZWJlbHIKCxIEUGFnZRgBDA')
                #alternative code
                # page db.get(db.key(self.request.get('key')))
                # The solution is to call/fetch the wanted object/query
      data = page.get() # or ... = Page.gql("GQL CODE").fetch(1)
      values = {'page': page}
      template_name = "simple_page.html"
      return self.response.out.write(self.renderPage(values))
    

    密钥只是从我的存储中随机取出的,它是已填充实体的真实现有密钥。 They idea is to load a page content dynamically into the doc via AJAX, problem is, that this handler returns an empty template. 无错误、200 HTTP代码、密钥存在等。 我完全崩溃了,对这样的问题有点恼火,因为我安静不知道故障可能在哪里。

    当做,

    EDIT: Changing the template values to there correct names, I now get the following erro:

        values = {'page': page, 'name': page.name,}
    AttributeError: 'NoneType' object has no attribute 'name'
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Nick Johnson    14 年前

    Your properties are called 'leftText', 'rightText', and 'imageURL', but you're trying to print out 'left_text', 'right_text' and 'image_url'. Django, in its infinite wisdom, simply returns an empty string when you try to access a property that doesn't exist, rather than throwing an exception.